blob: d0d6cd51843d15cd4e09a9f01143fa78c1f7dbde [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>
Daniel Vetter3b96a0b2016-06-21 10:54:22 +020042#include <drm/drm_auth.h>
Daniel Vetter7520a272016-08-15 16:07:02 +020043#include <drm/drm_framebuffer.h>
Dave Airlief453ba02008-11-07 14:05:41 -080044
Daniel Vetter8bd441b2014-01-23 15:35:24 +010045#include "drm_crtc_internal.h"
Daniel Vetter67d0ec42014-09-10 12:43:53 +020046#include "drm_internal.h"
Daniel Vetter8bd441b2014-01-23 15:35:24 +010047
Dave Airlief453ba02008-11-07 14:05:41 -080048/* Avoid boilerplate. I'm tired of typing. */
49#define DRM_ENUM_NAME_FN(fnname, list) \
Ville Syrjäläd20d3172013-06-07 15:43:07 +000050 const char *fnname(int val) \
Dave Airlief453ba02008-11-07 14:05:41 -080051 { \
52 int i; \
53 for (i = 0; i < ARRAY_SIZE(list); i++) { \
54 if (list[i].type == val) \
55 return list[i].name; \
56 } \
57 return "(unknown)"; \
58 }
59
60/*
61 * Global properties
62 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010063static const struct drm_prop_enum_list drm_dpms_enum_list[] = {
64 { DRM_MODE_DPMS_ON, "On" },
Dave Airlief453ba02008-11-07 14:05:41 -080065 { DRM_MODE_DPMS_STANDBY, "Standby" },
66 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
67 { DRM_MODE_DPMS_OFF, "Off" }
68};
69
70DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
71
Thierry Reding4dfd9092014-12-10 13:03:34 +010072static const struct drm_prop_enum_list drm_plane_type_enum_list[] = {
Rob Clark9922ab52014-04-01 20:16:57 -040073 { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
74 { DRM_PLANE_TYPE_PRIMARY, "Primary" },
75 { DRM_PLANE_TYPE_CURSOR, "Cursor" },
76};
77
Dave Airlief453ba02008-11-07 14:05:41 -080078/*
79 * Optional properties
80 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010081static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] = {
Jesse Barnes53bd8382009-07-01 10:04:40 -070082 { DRM_MODE_SCALE_NONE, "None" },
83 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
84 { DRM_MODE_SCALE_CENTER, "Center" },
85 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
Dave Airlief453ba02008-11-07 14:05:41 -080086};
87
Vandana Kannanff587e42014-06-11 10:46:48 +053088static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
89 { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
90 { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
91 { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
92};
93
Dave Airlief453ba02008-11-07 14:05:41 -080094/*
95 * Non-global properties, but "required" for certain connectors.
96 */
Thierry Reding4dfd9092014-12-10 13:03:34 +010097static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -080098 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
99 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
100 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
101};
102
103DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
104
Thierry Reding4dfd9092014-12-10 13:03:34 +0100105static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800106 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
107 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
108 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
109};
110
111DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
112 drm_dvi_i_subconnector_enum_list)
113
Thierry Reding4dfd9092014-12-10 13:03:34 +0100114static const struct drm_prop_enum_list drm_tv_select_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800115 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
116 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
117 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
118 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200119 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800120};
121
122DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
123
Thierry Reding4dfd9092014-12-10 13:03:34 +0100124static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] = {
Dave Airlief453ba02008-11-07 14:05:41 -0800125 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
126 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
127 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
128 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
Francisco Jerezaeaa1ad2009-08-02 04:19:19 +0200129 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
Dave Airlief453ba02008-11-07 14:05:41 -0800130};
131
132DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
133 drm_tv_subconnector_enum_list)
134
135struct drm_conn_prop_enum_list {
136 int type;
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000137 const char *name;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400138 struct ida ida;
Dave Airlief453ba02008-11-07 14:05:41 -0800139};
140
141/*
142 * Connector and encoder types.
143 */
Thierry Reding4dfd9092014-12-10 13:03:34 +0100144static struct drm_conn_prop_enum_list drm_connector_enum_list[] = {
145 { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400146 { DRM_MODE_CONNECTOR_VGA, "VGA" },
147 { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
148 { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
149 { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
150 { DRM_MODE_CONNECTOR_Composite, "Composite" },
151 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
152 { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
153 { DRM_MODE_CONNECTOR_Component, "Component" },
154 { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
155 { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
156 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
157 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
158 { DRM_MODE_CONNECTOR_TV, "TV" },
159 { DRM_MODE_CONNECTOR_eDP, "eDP" },
160 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300161 { DRM_MODE_CONNECTOR_DSI, "DSI" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700162 { DRM_MODE_CONNECTOR_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800163};
164
Thierry Reding4dfd9092014-12-10 13:03:34 +0100165static const struct drm_prop_enum_list drm_encoder_enum_list[] = {
166 { DRM_MODE_ENCODER_NONE, "None" },
Dave Airlief453ba02008-11-07 14:05:41 -0800167 { DRM_MODE_ENCODER_DAC, "DAC" },
168 { DRM_MODE_ENCODER_TMDS, "TMDS" },
169 { DRM_MODE_ENCODER_LVDS, "LVDS" },
170 { DRM_MODE_ENCODER_TVDAC, "TV" },
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200171 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
Shobhit Kumarb8923272013-08-27 15:12:13 +0300172 { DRM_MODE_ENCODER_DSI, "DSI" },
Dave Airlie182407a2014-05-02 11:09:54 +1000173 { DRM_MODE_ENCODER_DPMST, "DP MST" },
Eric Anholt0b27c022016-03-18 12:34:59 -0700174 { DRM_MODE_ENCODER_DPI, "DPI" },
Dave Airlief453ba02008-11-07 14:05:41 -0800175};
176
Thierry Reding4dfd9092014-12-10 13:03:34 +0100177static const struct drm_prop_enum_list drm_subpixel_enum_list[] = {
Jesse Barnesac1bb362014-02-10 15:32:44 -0800178 { SubPixelUnknown, "Unknown" },
179 { SubPixelHorizontalRGB, "Horizontal RGB" },
180 { SubPixelHorizontalBGR, "Horizontal BGR" },
181 { SubPixelVerticalRGB, "Vertical RGB" },
182 { SubPixelVerticalBGR, "Vertical BGR" },
183 { SubPixelNone, "None" },
184};
185
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400186void drm_connector_ida_init(void)
187{
188 int i;
189
190 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
191 ida_init(&drm_connector_enum_list[i].ida);
192}
193
194void drm_connector_ida_destroy(void)
195{
196 int i;
197
198 for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
199 ida_destroy(&drm_connector_enum_list[i].ida);
200}
201
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100202/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100203 * drm_get_connector_status_name - return a string for connector status
204 * @status: connector status to compute name of
205 *
206 * In contrast to the other drm_get_*_name functions this one here returns a
207 * const pointer and hence is threadsafe.
208 */
Ville Syrjäläd20d3172013-06-07 15:43:07 +0000209const char *drm_get_connector_status_name(enum drm_connector_status status)
Dave Airlief453ba02008-11-07 14:05:41 -0800210{
211 if (status == connector_status_connected)
212 return "connected";
213 else if (status == connector_status_disconnected)
214 return "disconnected";
215 else
216 return "unknown";
217}
Lespiau, Damiened7951d2013-05-10 12:36:42 +0000218EXPORT_SYMBOL(drm_get_connector_status_name);
Dave Airlief453ba02008-11-07 14:05:41 -0800219
Jesse Barnesac1bb362014-02-10 15:32:44 -0800220/**
221 * drm_get_subpixel_order_name - return a string for a given subpixel enum
222 * @order: enum of subpixel_order
223 *
224 * Note you could abuse this and return something out of bounds, but that
225 * would be a caller error. No unscrubbed user data should make it here.
226 */
227const char *drm_get_subpixel_order_name(enum subpixel_order order)
228{
229 return drm_subpixel_enum_list[order].name;
230}
231EXPORT_SYMBOL(drm_get_subpixel_order_name);
232
Dave Airlie2ee39452014-07-24 11:53:45 +1000233/*
234 * Internal function to assign a slot in the object idr and optionally
235 * register the object into the idr.
236 */
Daniel Vetter7520a272016-08-15 16:07:02 +0200237int drm_mode_object_get_reg(struct drm_device *dev,
238 struct drm_mode_object *obj,
239 uint32_t obj_type,
240 bool register_obj,
241 void (*obj_free_cb)(struct kref *kref))
Dave Airlie2ee39452014-07-24 11:53:45 +1000242{
243 int ret;
244
245 mutex_lock(&dev->mode_config.idr_mutex);
246 ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
247 if (ret >= 0) {
248 /*
249 * Set up the object linking under the protection of the idr
250 * lock so that other users can't see inconsistent state.
251 */
252 obj->id = ret;
253 obj->type = obj_type;
Dave Airlied0f37cf2016-04-15 15:10:36 +1000254 if (obj_free_cb) {
255 obj->free_cb = obj_free_cb;
256 kref_init(&obj->refcount);
257 }
Dave Airlie2ee39452014-07-24 11:53:45 +1000258 }
259 mutex_unlock(&dev->mode_config.idr_mutex);
260
261 return ret < 0 ? ret : 0;
262}
263
Dave Airlief453ba02008-11-07 14:05:41 -0800264/**
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100265 * drm_mode_object_get - allocate a new modeset identifier
Dave Airlief453ba02008-11-07 14:05:41 -0800266 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100267 * @obj: object pointer, used to generate unique ID
268 * @obj_type: object type
Dave Airlief453ba02008-11-07 14:05:41 -0800269 *
Dave Airlief453ba02008-11-07 14:05:41 -0800270 * Create a unique identifier based on @ptr in @dev's identifier space. Used
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100271 * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
272 * modeset identifiers are _not_ reference counted. Hence don't use this for
273 * reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800274 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100275 * Returns:
Lukas Wunner3c67d832015-10-15 11:56:56 +0200276 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800277 */
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100278int drm_mode_object_get(struct drm_device *dev,
279 struct drm_mode_object *obj, uint32_t obj_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800280{
Dave Airlied0f37cf2016-04-15 15:10:36 +1000281 return drm_mode_object_get_reg(dev, obj, obj_type, true, NULL);
Dave Airlie2ee39452014-07-24 11:53:45 +1000282}
Dave Airlief453ba02008-11-07 14:05:41 -0800283
Daniel Vetter7520a272016-08-15 16:07:02 +0200284void drm_mode_object_register(struct drm_device *dev,
285 struct drm_mode_object *obj)
Dave Airlie2ee39452014-07-24 11:53:45 +1000286{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000287 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie2ee39452014-07-24 11:53:45 +1000288 idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
Jesse Barnesad2563c2009-01-19 17:21:45 +1000289 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800290}
291
292/**
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000293 * drm_mode_object_unregister - free a modeset identifer
Dave Airlief453ba02008-11-07 14:05:41 -0800294 * @dev: DRM device
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100295 * @object: object to free
Dave Airlief453ba02008-11-07 14:05:41 -0800296 *
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000297 * Free @id from @dev's unique identifier pool.
298 * This function can be called multiple times, and guards against
299 * multiple removals.
300 * These modeset identifiers are _not_ reference counted. Hence don't use this
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100301 * for reference counted modeset objects like framebuffers.
Dave Airlief453ba02008-11-07 14:05:41 -0800302 */
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000303void drm_mode_object_unregister(struct drm_device *dev,
Daniel Vetter8bd441b2014-01-23 15:35:24 +0100304 struct drm_mode_object *object)
Dave Airlief453ba02008-11-07 14:05:41 -0800305{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000306 mutex_lock(&dev->mode_config.idr_mutex);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000307 if (object->id) {
308 idr_remove(&dev->mode_config.crtc_idr, object->id);
309 object->id = 0;
310 }
Jesse Barnesad2563c2009-01-19 17:21:45 +1000311 mutex_unlock(&dev->mode_config.idr_mutex);
Dave Airlief453ba02008-11-07 14:05:41 -0800312}
313
Daniel Vetter7520a272016-08-15 16:07:02 +0200314struct drm_mode_object *__drm_mode_object_find(struct drm_device *dev,
315 uint32_t id, uint32_t type)
Rob Clark98f75de2014-05-30 11:37:03 -0400316{
317 struct drm_mode_object *obj = NULL;
318
319 mutex_lock(&dev->mode_config.idr_mutex);
320 obj = idr_find(&dev->mode_config.crtc_idr, id);
Daniel Vetter168c02e2014-07-24 12:12:45 +0200321 if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
322 obj = NULL;
323 if (obj && obj->id != id)
324 obj = NULL;
Dave Airlie72fe90b2016-04-15 15:10:40 +1000325
326 if (obj && obj->free_cb) {
327 if (!kref_get_unless_zero(&obj->refcount))
328 obj = NULL;
329 }
Rob Clark98f75de2014-05-30 11:37:03 -0400330 mutex_unlock(&dev->mode_config.idr_mutex);
331
332 return obj;
333}
334
Daniel Vetter786b99e2012-12-02 21:53:40 +0100335/**
336 * drm_mode_object_find - look up a drm object with static lifetime
337 * @dev: drm device
338 * @id: id of the mode object
339 * @type: type of the mode object
340 *
Daniel Vetter059814222016-04-22 22:10:27 +0200341 * This function is used to look up a modeset object. It will acquire a
342 * reference for reference counted objects. This reference must be dropped again
343 * by callind drm_mode_object_unreference().
Daniel Vetter786b99e2012-12-02 21:53:40 +0100344 */
Daniel Vetter7a9c9062009-09-15 22:57:31 +0200345struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
346 uint32_t id, uint32_t type)
Dave Airlief453ba02008-11-07 14:05:41 -0800347{
Jesse Barnesad2563c2009-01-19 17:21:45 +1000348 struct drm_mode_object *obj = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800349
Daniel Vetter7520a272016-08-15 16:07:02 +0200350 obj = __drm_mode_object_find(dev, id, type);
Dave Airlief453ba02008-11-07 14:05:41 -0800351 return obj;
352}
353EXPORT_SYMBOL(drm_mode_object_find);
354
Daniel Vetter059814222016-04-22 22:10:27 +0200355/**
356 * drm_mode_object_unreference - decr the object refcnt
357 * @obj: mode_object
358 *
359 * This functions decrements the object's refcount if it is a refcounted modeset
360 * object. It is a no-op on any other object. This is used to drop references
361 * acquired with drm_mode_object_reference().
362 */
Dave Airlied0f37cf2016-04-15 15:10:36 +1000363void drm_mode_object_unreference(struct drm_mode_object *obj)
364{
365 if (obj->free_cb) {
366 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
367 kref_put(&obj->refcount, obj->free_cb);
368 }
369}
370EXPORT_SYMBOL(drm_mode_object_unreference);
371
372/**
Daniel Vetter059814222016-04-22 22:10:27 +0200373 * drm_mode_object_reference - incr the object refcnt
Dave Airlied0f37cf2016-04-15 15:10:36 +1000374 * @obj: mode_object
375 *
Daniel Vetter059814222016-04-22 22:10:27 +0200376 * This functions increments the object's refcount if it is a refcounted modeset
377 * object. It is a no-op on any other object. References should be dropped again
378 * by calling drm_mode_object_unreference().
Dave Airlied0f37cf2016-04-15 15:10:36 +1000379 */
380void drm_mode_object_reference(struct drm_mode_object *obj)
381{
382 if (obj->free_cb) {
383 DRM_DEBUG("OBJ ID: %d (%d)\n", obj->id, atomic_read(&obj->refcount.refcount));
384 kref_get(&obj->refcount);
385 }
386}
387EXPORT_SYMBOL(drm_mode_object_reference);
388
Lukas Wunner6a0d9522016-06-08 18:47:27 +0200389/**
390 * drm_crtc_force_disable - Forcibly turn off a CRTC
391 * @crtc: CRTC to turn off
392 *
393 * Returns:
394 * Zero on success, error code on failure.
395 */
396int drm_crtc_force_disable(struct drm_crtc *crtc)
397{
398 struct drm_mode_set set = {
399 .crtc = crtc,
400 };
401
402 return drm_mode_set_config_internal(&set);
403}
404EXPORT_SYMBOL(drm_crtc_force_disable);
405
406/**
407 * drm_crtc_force_disable_all - Forcibly turn off all enabled CRTCs
408 * @dev: DRM device whose CRTCs to turn off
409 *
410 * Drivers may want to call this on unload to ensure that all displays are
411 * unlit and the GPU is in a consistent, low power state. Takes modeset locks.
412 *
413 * Returns:
414 * Zero on success, error code on failure.
415 */
416int drm_crtc_force_disable_all(struct drm_device *dev)
417{
418 struct drm_crtc *crtc;
419 int ret = 0;
420
421 drm_modeset_lock_all(dev);
422 drm_for_each_crtc(crtc, dev)
423 if (crtc->enabled) {
424 ret = drm_crtc_force_disable(crtc);
425 if (ret)
426 goto out;
427 }
428out:
429 drm_modeset_unlock_all(dev);
430 return ret;
431}
432EXPORT_SYMBOL(drm_crtc_force_disable_all);
433
Rob Clark51fd3712013-11-19 12:10:12 -0500434DEFINE_WW_CLASS(crtc_ww_class);
435
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200436static unsigned int drm_num_crtcs(struct drm_device *dev)
437{
438 unsigned int num = 0;
439 struct drm_crtc *tmp;
440
441 drm_for_each_crtc(tmp, dev) {
442 num++;
443 }
444
445 return num;
446}
447
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200448static int drm_crtc_register_all(struct drm_device *dev)
449{
450 struct drm_crtc *crtc;
451 int ret = 0;
452
453 drm_for_each_crtc(crtc, dev) {
454 if (crtc->funcs->late_register)
455 ret = crtc->funcs->late_register(crtc);
456 if (ret)
457 return ret;
458 }
459
460 return 0;
461}
462
463static void drm_crtc_unregister_all(struct drm_device *dev)
464{
465 struct drm_crtc *crtc;
466
467 drm_for_each_crtc(crtc, dev) {
468 if (crtc->funcs->early_unregister)
469 crtc->funcs->early_unregister(crtc);
470 }
471}
472
Dave Airlief453ba02008-11-07 14:05:41 -0800473/**
Matt Ropere13161a2014-04-01 15:22:38 -0700474 * drm_crtc_init_with_planes - Initialise a new CRTC object with
475 * specified primary and cursor planes.
Dave Airlief453ba02008-11-07 14:05:41 -0800476 * @dev: DRM device
477 * @crtc: CRTC object to init
Matt Ropere13161a2014-04-01 15:22:38 -0700478 * @primary: Primary plane for CRTC
479 * @cursor: Cursor plane for CRTC
Dave Airlief453ba02008-11-07 14:05:41 -0800480 * @funcs: callbacks for the new CRTC
Ville Syrjäläf9882872015-12-09 16:19:31 +0200481 * @name: printf style format string for the CRTC name, or NULL for default name
Dave Airlief453ba02008-11-07 14:05:41 -0800482 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000483 * Inits a new object created as base part of a driver crtc object.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200484 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100485 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200486 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800487 */
Matt Ropere13161a2014-04-01 15:22:38 -0700488int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
489 struct drm_plane *primary,
Matt Roperfc1d3e42014-06-10 08:28:11 -0700490 struct drm_plane *cursor,
Ville Syrjäläf9882872015-12-09 16:19:31 +0200491 const struct drm_crtc_funcs *funcs,
492 const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800493{
Rob Clark51fd3712013-11-19 12:10:12 -0500494 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200495 int ret;
496
Benjamin Gaignard522cf912015-03-17 12:05:29 +0100497 WARN_ON(primary && primary->type != DRM_PLANE_TYPE_PRIMARY);
498 WARN_ON(cursor && cursor->type != DRM_PLANE_TYPE_CURSOR);
499
Dave Airlief453ba02008-11-07 14:05:41 -0800500 crtc->dev = dev;
501 crtc->funcs = funcs;
502
Daniel Vetter3b24f7d2016-06-08 14:19:00 +0200503 INIT_LIST_HEAD(&crtc->commit_list);
504 spin_lock_init(&crtc->commit_lock);
505
Rob Clark51fd3712013-11-19 12:10:12 -0500506 drm_modeset_lock_init(&crtc->mutex);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200507 ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
508 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100509 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800510
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200511 if (name) {
512 va_list ap;
513
514 va_start(ap, name);
515 crtc->name = kvasprintf(GFP_KERNEL, name, ap);
516 va_end(ap);
517 } else {
518 crtc->name = kasprintf(GFP_KERNEL, "crtc-%d",
519 drm_num_crtcs(dev));
520 }
521 if (!crtc->name) {
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000522 drm_mode_object_unregister(dev, &crtc->base);
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200523 return -ENOMEM;
524 }
525
Paulo Zanonibffd9de02012-05-15 18:09:05 -0300526 crtc->base.properties = &crtc->properties;
527
Rob Clark51fd3712013-11-19 12:10:12 -0500528 list_add_tail(&crtc->head, &config->crtc_list);
Chris Wilson490d3d12016-05-27 20:05:00 +0100529 crtc->index = config->num_crtc++;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200530
Matt Ropere13161a2014-04-01 15:22:38 -0700531 crtc->primary = primary;
Matt Roperfc1d3e42014-06-10 08:28:11 -0700532 crtc->cursor = cursor;
Matt Ropere13161a2014-04-01 15:22:38 -0700533 if (primary)
534 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Roperfc1d3e42014-06-10 08:28:11 -0700535 if (cursor)
536 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
Matt Ropere13161a2014-04-01 15:22:38 -0700537
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100538 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
539 drm_object_attach_property(&crtc->base, config->prop_active, 0);
Daniel Stone955f3c32015-05-25 19:11:52 +0100540 drm_object_attach_property(&crtc->base, config->prop_mode_id, 0);
Daniel Vettereab3bbe2015-01-22 16:36:21 +0100541 }
542
Daniel Vetterbaf698b2014-11-12 11:59:47 +0100543 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -0800544}
Matt Ropere13161a2014-04-01 15:22:38 -0700545EXPORT_SYMBOL(drm_crtc_init_with_planes);
Dave Airlief453ba02008-11-07 14:05:41 -0800546
547/**
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000548 * drm_crtc_cleanup - Clean up the core crtc usage
Dave Airlief453ba02008-11-07 14:05:41 -0800549 * @crtc: CRTC to cleanup
550 *
Ville Syrjäläad6f5c32013-06-05 12:39:55 +0000551 * This function cleans up @crtc and removes it from the DRM mode setting
552 * core. Note that the function does *not* free the crtc structure itself,
553 * this is the responsibility of the caller.
Dave Airlief453ba02008-11-07 14:05:41 -0800554 */
555void drm_crtc_cleanup(struct drm_crtc *crtc)
556{
557 struct drm_device *dev = crtc->dev;
558
Chris Wilson490d3d12016-05-27 20:05:00 +0100559 /* Note that the crtc_list is considered to be static; should we
560 * remove the drm_crtc at runtime we would have to decrement all
561 * the indices on the drm_crtc after us in the crtc_list.
562 */
563
Sachin Kamat9e1c1562012-11-19 09:44:56 +0000564 kfree(crtc->gamma_store);
565 crtc->gamma_store = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800566
Rob Clark51fd3712013-11-19 12:10:12 -0500567 drm_modeset_lock_fini(&crtc->mutex);
568
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000569 drm_mode_object_unregister(dev, &crtc->base);
Dave Airlief453ba02008-11-07 14:05:41 -0800570 list_del(&crtc->head);
571 dev->mode_config.num_crtc--;
Thierry Reding3009c032014-11-25 12:09:49 +0100572
573 WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
574 if (crtc->state && crtc->funcs->atomic_destroy_state)
575 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100576
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +0200577 kfree(crtc->name);
578
Thierry Redinga18c0af2014-12-10 11:38:49 +0100579 memset(crtc, 0, sizeof(*crtc));
Dave Airlief453ba02008-11-07 14:05:41 -0800580}
581EXPORT_SYMBOL(drm_crtc_cleanup);
582
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100583/*
Dave Airlief453ba02008-11-07 14:05:41 -0800584 * drm_mode_remove - remove and free a mode
585 * @connector: connector list to modify
586 * @mode: mode to remove
587 *
Dave Airlief453ba02008-11-07 14:05:41 -0800588 * Remove @mode from @connector's mode list, then free it.
589 */
Lespiau, Damien86f422d2013-08-20 00:53:06 +0100590static void drm_mode_remove(struct drm_connector *connector,
591 struct drm_display_mode *mode)
Dave Airlief453ba02008-11-07 14:05:41 -0800592{
593 list_del(&mode->head);
Sascha Hauer554f1d72012-02-01 11:38:19 +0100594 drm_mode_destroy(connector->dev, mode);
Dave Airlief453ba02008-11-07 14:05:41 -0800595}
Dave Airlief453ba02008-11-07 14:05:41 -0800596
597/**
Boris Brezillonb5571e92014-07-22 12:09:10 +0200598 * drm_display_info_set_bus_formats - set the supported bus formats
599 * @info: display info to store bus formats in
Boris Brezillone37bfa12015-01-23 21:09:30 +0100600 * @formats: array containing the supported bus formats
601 * @num_formats: the number of entries in the fmts array
Boris Brezillonb5571e92014-07-22 12:09:10 +0200602 *
603 * Store the supported bus formats in display info structure.
604 * See MEDIA_BUS_FMT_* definitions in include/uapi/linux/media-bus-format.h for
605 * a full list of available formats.
606 */
607int drm_display_info_set_bus_formats(struct drm_display_info *info,
608 const u32 *formats,
609 unsigned int num_formats)
610{
611 u32 *fmts = NULL;
612
613 if (!formats && num_formats)
614 return -EINVAL;
615
616 if (formats && num_formats) {
617 fmts = kmemdup(formats, sizeof(*formats) * num_formats,
618 GFP_KERNEL);
Dan Carpenter944579c2015-01-28 09:43:35 +0300619 if (!fmts)
Boris Brezillonb5571e92014-07-22 12:09:10 +0200620 return -ENOMEM;
621 }
622
623 kfree(info->bus_formats);
624 info->bus_formats = fmts;
625 info->num_bus_formats = num_formats;
626
627 return 0;
628}
629EXPORT_SYMBOL(drm_display_info_set_bus_formats);
630
631/**
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200632 * drm_connector_get_cmdline_mode - reads the user's cmdline mode
633 * @connector: connector to quwery
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200634 *
635 * The kernel supports per-connector configration of its consoles through
636 * use of the video= parameter. This function parses that option and
637 * extracts the user's specified mode (or enable/disable status) for a
638 * particular connector. This is typically only used during the early fbdev
639 * setup.
640 */
641static void drm_connector_get_cmdline_mode(struct drm_connector *connector)
642{
643 struct drm_cmdline_mode *mode = &connector->cmdline_mode;
644 char *option = NULL;
645
646 if (fb_get_options(connector->name, &option))
647 return;
648
649 if (!drm_mode_parse_command_line_for_connector(option,
650 connector,
651 mode))
652 return;
653
654 if (mode->force) {
655 const char *s;
656
657 switch (mode->force) {
658 case DRM_FORCE_OFF:
659 s = "OFF";
660 break;
661 case DRM_FORCE_ON_DIGITAL:
662 s = "ON - dig";
663 break;
664 default:
665 case DRM_FORCE_ON:
666 s = "ON";
667 break;
668 }
669
670 DRM_INFO("forcing %s connector %s\n", connector->name, s);
671 connector->force = mode->force;
672 }
673
674 DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
675 connector->name,
676 mode->xres, mode->yres,
677 mode->refresh_specified ? mode->refresh : 60,
678 mode->rb ? " reduced blanking" : "",
679 mode->margins ? " with margins" : "",
680 mode->interlace ? " interlaced" : "");
681}
682
Dave Airlieb164d312016-04-27 11:10:09 +1000683static void drm_connector_free(struct kref *kref)
684{
685 struct drm_connector *connector =
686 container_of(kref, struct drm_connector, base.refcount);
687 struct drm_device *dev = connector->dev;
688
689 drm_mode_object_unregister(dev, &connector->base);
690 connector->funcs->destroy(connector);
691}
692
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200693/**
Dave Airlief453ba02008-11-07 14:05:41 -0800694 * drm_connector_init - Init a preallocated connector
695 * @dev: DRM device
696 * @connector: the connector to init
697 * @funcs: callbacks for this connector
Daniel Vetter065a50ed2012-12-02 00:09:18 +0100698 * @connector_type: user visible type of the connector
Dave Airlief453ba02008-11-07 14:05:41 -0800699 *
Dave Airlief453ba02008-11-07 14:05:41 -0800700 * Initialises a preallocated connector. Connectors should be
701 * subclassed as part of driver connector objects.
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200702 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100703 * Returns:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200704 * Zero on success, error code on failure.
Dave Airlief453ba02008-11-07 14:05:41 -0800705 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200706int drm_connector_init(struct drm_device *dev,
707 struct drm_connector *connector,
708 const struct drm_connector_funcs *funcs,
709 int connector_type)
Dave Airlief453ba02008-11-07 14:05:41 -0800710{
Rob Clarkae16c592014-12-18 16:01:54 -0500711 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200712 int ret;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400713 struct ida *connector_ida =
714 &drm_connector_enum_list[connector_type].ida;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200715
Daniel Vetter84849902012-12-02 00:28:11 +0100716 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800717
Dave Airlieb164d312016-04-27 11:10:09 +1000718 ret = drm_mode_object_get_reg(dev, &connector->base,
719 DRM_MODE_OBJECT_CONNECTOR,
720 false, drm_connector_free);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200721 if (ret)
Jani Nikula2abdd312014-05-14 16:58:19 +0300722 goto out_unlock;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200723
Paulo Zanoni7e3bdf42012-05-15 18:09:01 -0300724 connector->base.properties = &connector->properties;
Dave Airlief453ba02008-11-07 14:05:41 -0800725 connector->dev = dev;
726 connector->funcs = funcs;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100727
Daniel Vetter69425592016-07-19 18:25:01 +0200728 ret = ida_simple_get(&config->connector_ida, 0, 0, GFP_KERNEL);
729 if (ret < 0)
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100730 goto out_put;
Daniel Vetter69425592016-07-19 18:25:01 +0200731 connector->index = ret;
732 ret = 0;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100733
Dave Airlief453ba02008-11-07 14:05:41 -0800734 connector->connector_type = connector_type;
735 connector->connector_type_id =
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400736 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
737 if (connector->connector_type_id < 0) {
738 ret = connector->connector_type_id;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100739 goto out_put_id;
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400740 }
Jani Nikula2abdd312014-05-14 16:58:19 +0300741 connector->name =
742 kasprintf(GFP_KERNEL, "%s-%d",
743 drm_connector_enum_list[connector_type].name,
744 connector->connector_type_id);
745 if (!connector->name) {
746 ret = -ENOMEM;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100747 goto out_put_type_id;
Jani Nikula2abdd312014-05-14 16:58:19 +0300748 }
749
Dave Airlief453ba02008-11-07 14:05:41 -0800750 INIT_LIST_HEAD(&connector->probed_modes);
751 INIT_LIST_HEAD(&connector->modes);
752 connector->edid_blob_ptr = NULL;
Daniel Vetter5e2cb2f2012-10-23 18:23:35 +0000753 connector->status = connector_status_unknown;
Dave Airlief453ba02008-11-07 14:05:41 -0800754
Chris Wilsoneaf99c72014-08-06 10:08:32 +0200755 drm_connector_get_cmdline_mode(connector);
756
Daniel Vetterc7eb76f2014-11-19 18:38:06 +0100757 /* We should add connectors at the end to avoid upsetting the connector
758 * index too much. */
Rob Clarkae16c592014-12-18 16:01:54 -0500759 list_add_tail(&connector->head, &config->connector_list);
760 config->num_connector++;
Dave Airlief453ba02008-11-07 14:05:41 -0800761
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200762 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
Rob Clark58495562012-10-11 20:50:56 -0500763 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500764 config->edid_property,
Thomas Hellstroma7331e52011-10-22 10:36:19 +0200765 0);
Dave Airlief453ba02008-11-07 14:05:41 -0800766
Rob Clark58495562012-10-11 20:50:56 -0500767 drm_object_attach_property(&connector->base,
Rob Clarkae16c592014-12-18 16:01:54 -0500768 config->dpms_property, 0);
769
770 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
771 drm_object_attach_property(&connector->base, config->prop_crtc_id, 0);
772 }
Dave Airlief453ba02008-11-07 14:05:41 -0800773
Thomas Wood30f65702014-06-18 17:52:32 +0100774 connector->debugfs_entry = NULL;
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100775out_put_type_id:
776 if (ret)
777 ida_remove(connector_ida, connector->connector_type_id);
778out_put_id:
779 if (ret)
Daniel Vetter69425592016-07-19 18:25:01 +0200780 ida_remove(&config->connector_ida, connector->index);
Jani Nikula2abdd312014-05-14 16:58:19 +0300781out_put:
782 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000783 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300784
785out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +0100786 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200787
788 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -0800789}
790EXPORT_SYMBOL(drm_connector_init);
791
792/**
793 * drm_connector_cleanup - cleans up an initialised connector
794 * @connector: connector to cleanup
795 *
Dave Airlief453ba02008-11-07 14:05:41 -0800796 * Cleans up the connector but doesn't free the object.
797 */
798void drm_connector_cleanup(struct drm_connector *connector)
799{
800 struct drm_device *dev = connector->dev;
801 struct drm_display_mode *mode, *t;
802
Chris Wilson80de3462016-06-15 13:17:49 +0100803 /* The connector should have been removed from userspace long before
804 * it is finally destroyed.
805 */
806 if (WARN_ON(connector->registered))
807 drm_connector_unregister(connector);
808
Dave Airlie40d9b042014-10-20 16:29:33 +1000809 if (connector->tile_group) {
810 drm_mode_put_tile_group(dev, connector->tile_group);
811 connector->tile_group = NULL;
812 }
813
Dave Airlief453ba02008-11-07 14:05:41 -0800814 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
815 drm_mode_remove(connector, mode);
816
817 list_for_each_entry_safe(mode, t, &connector->modes, head)
818 drm_mode_remove(connector, mode);
819
Ilia Mirkinb21e3af2013-08-07 22:34:48 -0400820 ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
821 connector->connector_type_id);
822
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100823 ida_remove(&dev->mode_config.connector_ida,
Daniel Vetter69425592016-07-19 18:25:01 +0200824 connector->index);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +0100825
Boris Brezillonb5571e92014-07-22 12:09:10 +0200826 kfree(connector->display_info.bus_formats);
Dave Airlie7c8f6d22016-04-15 15:10:32 +1000827 drm_mode_object_unregister(dev, &connector->base);
Jani Nikula2abdd312014-05-14 16:58:19 +0300828 kfree(connector->name);
829 connector->name = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -0800830 list_del(&connector->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +0000831 dev->mode_config.num_connector--;
Thierry Reding3009c032014-11-25 12:09:49 +0100832
833 WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
834 if (connector->state && connector->funcs->atomic_destroy_state)
835 connector->funcs->atomic_destroy_state(connector,
836 connector->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +0100837
838 memset(connector, 0, sizeof(*connector));
Dave Airlief453ba02008-11-07 14:05:41 -0800839}
840EXPORT_SYMBOL(drm_connector_cleanup);
841
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100842/**
Thomas Wood34ea3d32014-05-29 16:57:41 +0100843 * drm_connector_register - register a connector
844 * @connector: the connector to register
845 *
846 * Register userspace interfaces for a connector
847 *
848 * Returns:
849 * Zero on success, error code on failure.
850 */
851int drm_connector_register(struct drm_connector *connector)
852{
Thomas Wood30f65702014-06-18 17:52:32 +0100853 int ret;
854
Chris Wilson40daac62016-06-15 13:17:48 +0100855 if (connector->registered)
856 return 0;
857
Thomas Wood30f65702014-06-18 17:52:32 +0100858 ret = drm_sysfs_connector_add(connector);
859 if (ret)
860 return ret;
861
862 ret = drm_debugfs_connector_add(connector);
863 if (ret) {
Chris Wilsonaaf285e2016-06-15 13:17:47 +0100864 goto err_sysfs;
865 }
866
867 if (connector->funcs->late_register) {
868 ret = connector->funcs->late_register(connector);
869 if (ret)
870 goto err_debugfs;
Thomas Wood30f65702014-06-18 17:52:32 +0100871 }
872
Daniel Vetterfdf2c852016-05-06 14:55:02 +0200873 drm_mode_object_register(connector->dev, &connector->base);
874
Chris Wilson40daac62016-06-15 13:17:48 +0100875 connector->registered = true;
Thomas Wood30f65702014-06-18 17:52:32 +0100876 return 0;
Chris Wilsonaaf285e2016-06-15 13:17:47 +0100877
878err_debugfs:
879 drm_debugfs_connector_remove(connector);
880err_sysfs:
881 drm_sysfs_connector_remove(connector);
882 return ret;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100883}
884EXPORT_SYMBOL(drm_connector_register);
885
886/**
887 * drm_connector_unregister - unregister a connector
888 * @connector: the connector to unregister
889 *
890 * Unregister userspace interfaces for a connector
891 */
892void drm_connector_unregister(struct drm_connector *connector)
893{
Chris Wilson40daac62016-06-15 13:17:48 +0100894 if (!connector->registered)
895 return;
896
Chris Wilsonaaf285e2016-06-15 13:17:47 +0100897 if (connector->funcs->early_unregister)
898 connector->funcs->early_unregister(connector);
899
Thomas Wood34ea3d32014-05-29 16:57:41 +0100900 drm_sysfs_connector_remove(connector);
Thomas Wood30f65702014-06-18 17:52:32 +0100901 drm_debugfs_connector_remove(connector);
Chris Wilson40daac62016-06-15 13:17:48 +0100902
903 connector->registered = false;
Thomas Wood34ea3d32014-05-29 16:57:41 +0100904}
905EXPORT_SYMBOL(drm_connector_unregister);
906
Chris Wilson61005982016-07-13 17:39:07 +0100907static void drm_connector_unregister_all(struct drm_device *dev)
908{
909 struct drm_connector *connector;
910
911 /* FIXME: taking the mode config mutex ends up in a clash with sysfs */
912 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
913 drm_connector_unregister(connector);
914}
915
Chris Wilson041401f2016-06-24 15:36:18 +0100916static int drm_connector_register_all(struct drm_device *dev)
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +0300917{
918 struct drm_connector *connector;
919 int ret;
920
Daniel Vetter5c6c2012016-08-04 18:35:48 +0200921 /* FIXME: taking the mode config mutex ends up in a clash with
922 * fbcon/backlight registration */
923 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +0300924 ret = drm_connector_register(connector);
925 if (ret)
926 goto err;
927 }
928
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +0300929 return 0;
930
931err:
932 mutex_unlock(&dev->mode_config.mutex);
933 drm_connector_unregister_all(dev);
934 return ret;
935}
Alexey Brodkin54d2c2d2016-04-19 15:24:51 +0300936
Benjamin Gaignard79190ea2016-06-21 16:37:09 +0200937static int drm_encoder_register_all(struct drm_device *dev)
938{
939 struct drm_encoder *encoder;
940 int ret = 0;
941
942 drm_for_each_encoder(encoder, dev) {
943 if (encoder->funcs->late_register)
944 ret = encoder->funcs->late_register(encoder);
945 if (ret)
946 return ret;
947 }
948
949 return 0;
950}
951
952static void drm_encoder_unregister_all(struct drm_device *dev)
953{
954 struct drm_encoder *encoder;
955
956 drm_for_each_encoder(encoder, dev) {
957 if (encoder->funcs->early_unregister)
958 encoder->funcs->early_unregister(encoder);
959 }
960}
961
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100962/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100963 * drm_encoder_init - Init a preallocated encoder
964 * @dev: drm device
965 * @encoder: the encoder to init
966 * @funcs: callbacks for this encoder
967 * @encoder_type: user visible type of the encoder
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200968 * @name: printf style format string for the encoder name, or NULL for default name
Daniel Vetterc8e32cc2014-03-10 21:33:02 +0100969 *
970 * Initialises a preallocated encoder. Encoder should be
971 * subclassed as part of driver encoder objects.
972 *
973 * Returns:
974 * Zero on success, error code on failure.
975 */
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200976int drm_encoder_init(struct drm_device *dev,
Dave Airliecbc7e222012-02-20 14:16:40 +0000977 struct drm_encoder *encoder,
978 const struct drm_encoder_funcs *funcs,
Ville Syrjälä13a3d912015-12-09 16:20:18 +0200979 int encoder_type, const char *name, ...)
Dave Airlief453ba02008-11-07 14:05:41 -0800980{
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200981 int ret;
982
Daniel Vetter84849902012-12-02 00:28:11 +0100983 drm_modeset_lock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -0800984
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200985 ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
986 if (ret)
Jani Nikulae5748942014-05-14 16:58:20 +0300987 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -0800988
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +0200989 encoder->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -0800990 encoder->encoder_type = encoder_type;
991 encoder->funcs = funcs;
Ville Syrjälä86bf5462015-12-08 18:41:52 +0200992 if (name) {
993 va_list ap;
994
995 va_start(ap, name);
996 encoder->name = kvasprintf(GFP_KERNEL, name, ap);
997 va_end(ap);
998 } else {
999 encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
1000 drm_encoder_enum_list[encoder_type].name,
1001 encoder->base.id);
1002 }
Jani Nikulae5748942014-05-14 16:58:20 +03001003 if (!encoder->name) {
1004 ret = -ENOMEM;
1005 goto out_put;
1006 }
Dave Airlief453ba02008-11-07 14:05:41 -08001007
1008 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001009 encoder->index = dev->mode_config.num_encoder++;
Dave Airlief453ba02008-11-07 14:05:41 -08001010
Jani Nikulae5748942014-05-14 16:58:20 +03001011out_put:
1012 if (ret)
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001013 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001014
1015out_unlock:
Daniel Vetter84849902012-12-02 00:28:11 +01001016 drm_modeset_unlock_all(dev);
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001017
1018 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08001019}
1020EXPORT_SYMBOL(drm_encoder_init);
1021
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001022/**
1023 * drm_encoder_cleanup - cleans up an initialised encoder
1024 * @encoder: encoder to cleanup
1025 *
1026 * Cleans up the encoder but doesn't free the object.
1027 */
Dave Airlief453ba02008-11-07 14:05:41 -08001028void drm_encoder_cleanup(struct drm_encoder *encoder)
1029{
1030 struct drm_device *dev = encoder->dev;
Thierry Reding4dfd9092014-12-10 13:03:34 +01001031
Chris Wilson490d3d12016-05-27 20:05:00 +01001032 /* Note that the encoder_list is considered to be static; should we
1033 * remove the drm_encoder at runtime we would have to decrement all
1034 * the indices on the drm_encoder after us in the encoder_list.
1035 */
1036
Daniel Vetter84849902012-12-02 00:28:11 +01001037 drm_modeset_lock_all(dev);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001038 drm_mode_object_unregister(dev, &encoder->base);
Jani Nikulae5748942014-05-14 16:58:20 +03001039 kfree(encoder->name);
Dave Airlief453ba02008-11-07 14:05:41 -08001040 list_del(&encoder->head);
Joonyoung Shim6380c502011-08-27 02:06:21 +00001041 dev->mode_config.num_encoder--;
Daniel Vetter84849902012-12-02 00:28:11 +01001042 drm_modeset_unlock_all(dev);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001043
1044 memset(encoder, 0, sizeof(*encoder));
Dave Airlief453ba02008-11-07 14:05:41 -08001045}
1046EXPORT_SYMBOL(drm_encoder_cleanup);
1047
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001048static unsigned int drm_num_planes(struct drm_device *dev)
1049{
1050 unsigned int num = 0;
1051 struct drm_plane *tmp;
1052
1053 drm_for_each_plane(tmp, dev) {
1054 num++;
1055 }
1056
1057 return num;
1058}
1059
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001060/**
Matt Roperdc415ff2014-04-01 15:22:36 -07001061 * drm_universal_plane_init - Initialize a new universal plane object
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001062 * @dev: DRM device
1063 * @plane: plane object to init
1064 * @possible_crtcs: bitmask of possible CRTCs
1065 * @funcs: callbacks for the new plane
Daniel Vetter62cacc72016-08-12 22:48:37 +02001066 * @formats: array of supported formats (DRM_FORMAT\_\*)
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001067 * @format_count: number of elements in @formats
Matt Roperdc415ff2014-04-01 15:22:36 -07001068 * @type: type of plane (overlay, primary, cursor)
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001069 * @name: printf style format string for the plane name, or NULL for default name
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001070 *
Matt Roperdc415ff2014-04-01 15:22:36 -07001071 * Initializes a plane object of type @type.
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001072 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001073 * Returns:
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001074 * Zero on success, error code on failure.
1075 */
Matt Roperdc415ff2014-04-01 15:22:36 -07001076int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
1077 unsigned long possible_crtcs,
1078 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001079 const uint32_t *formats, unsigned int format_count,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001080 enum drm_plane_type type,
1081 const char *name, ...)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001082{
Rob Clark6b4959f2014-12-18 16:01:53 -05001083 struct drm_mode_config *config = &dev->mode_config;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001084 int ret;
1085
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001086 ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
1087 if (ret)
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001088 return ret;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02001089
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01001090 drm_modeset_lock_init(&plane->mutex);
1091
Rob Clark4d939142012-05-17 02:23:27 -06001092 plane->base.properties = &plane->properties;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001093 plane->dev = dev;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001094 plane->funcs = funcs;
Thierry Reding2f6c5382014-12-10 13:03:36 +01001095 plane->format_types = kmalloc_array(format_count, sizeof(uint32_t),
1096 GFP_KERNEL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001097 if (!plane->format_types) {
1098 DRM_DEBUG_KMS("out of memory when allocating plane\n");
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001099 drm_mode_object_unregister(dev, &plane->base);
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001100 return -ENOMEM;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001101 }
1102
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001103 if (name) {
1104 va_list ap;
1105
1106 va_start(ap, name);
1107 plane->name = kvasprintf(GFP_KERNEL, name, ap);
1108 va_end(ap);
1109 } else {
1110 plane->name = kasprintf(GFP_KERNEL, "plane-%d",
1111 drm_num_planes(dev));
1112 }
1113 if (!plane->name) {
1114 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001115 drm_mode_object_unregister(dev, &plane->base);
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001116 return -ENOMEM;
1117 }
1118
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001119 memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001120 plane->format_count = format_count;
1121 plane->possible_crtcs = possible_crtcs;
Matt Roperdc415ff2014-04-01 15:22:36 -07001122 plane->type = type;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001123
Rob Clark6b4959f2014-12-18 16:01:53 -05001124 list_add_tail(&plane->head, &config->plane_list);
Chris Wilson490d3d12016-05-27 20:05:00 +01001125 plane->index = config->num_total_plane++;
Matt Roperdc415ff2014-04-01 15:22:36 -07001126 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
Rob Clark6b4959f2014-12-18 16:01:53 -05001127 config->num_overlay_plane++;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001128
Rob Clark9922ab52014-04-01 20:16:57 -04001129 drm_object_attach_property(&plane->base,
Rob Clark6b4959f2014-12-18 16:01:53 -05001130 config->plane_type_property,
Rob Clark9922ab52014-04-01 20:16:57 -04001131 plane->type);
1132
Rob Clark6b4959f2014-12-18 16:01:53 -05001133 if (drm_core_check_feature(dev, DRIVER_ATOMIC)) {
1134 drm_object_attach_property(&plane->base, config->prop_fb_id, 0);
1135 drm_object_attach_property(&plane->base, config->prop_crtc_id, 0);
1136 drm_object_attach_property(&plane->base, config->prop_crtc_x, 0);
1137 drm_object_attach_property(&plane->base, config->prop_crtc_y, 0);
1138 drm_object_attach_property(&plane->base, config->prop_crtc_w, 0);
1139 drm_object_attach_property(&plane->base, config->prop_crtc_h, 0);
1140 drm_object_attach_property(&plane->base, config->prop_src_x, 0);
1141 drm_object_attach_property(&plane->base, config->prop_src_y, 0);
1142 drm_object_attach_property(&plane->base, config->prop_src_w, 0);
1143 drm_object_attach_property(&plane->base, config->prop_src_h, 0);
1144 }
1145
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001146 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001147}
Matt Roperdc415ff2014-04-01 15:22:36 -07001148EXPORT_SYMBOL(drm_universal_plane_init);
1149
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001150static int drm_plane_register_all(struct drm_device *dev)
1151{
1152 struct drm_plane *plane;
1153 int ret = 0;
1154
1155 drm_for_each_plane(plane, dev) {
1156 if (plane->funcs->late_register)
1157 ret = plane->funcs->late_register(plane);
1158 if (ret)
1159 return ret;
1160 }
1161
1162 return 0;
1163}
1164
1165static void drm_plane_unregister_all(struct drm_device *dev)
1166{
1167 struct drm_plane *plane;
1168
1169 drm_for_each_plane(plane, dev) {
1170 if (plane->funcs->early_unregister)
1171 plane->funcs->early_unregister(plane);
1172 }
1173}
1174
Matt Roperdc415ff2014-04-01 15:22:36 -07001175/**
1176 * drm_plane_init - Initialize a legacy plane
1177 * @dev: DRM device
1178 * @plane: plane object to init
1179 * @possible_crtcs: bitmask of possible CRTCs
1180 * @funcs: callbacks for the new plane
Daniel Vetter62cacc72016-08-12 22:48:37 +02001181 * @formats: array of supported formats (DRM_FORMAT\_\*)
Matt Roperdc415ff2014-04-01 15:22:36 -07001182 * @format_count: number of elements in @formats
1183 * @is_primary: plane type (primary vs overlay)
1184 *
1185 * Legacy API to initialize a DRM plane.
1186 *
1187 * New drivers should call drm_universal_plane_init() instead.
1188 *
1189 * Returns:
1190 * Zero on success, error code on failure.
1191 */
1192int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
1193 unsigned long possible_crtcs,
1194 const struct drm_plane_funcs *funcs,
Thierry Reding45e37432015-08-12 16:54:28 +02001195 const uint32_t *formats, unsigned int format_count,
Matt Roperdc415ff2014-04-01 15:22:36 -07001196 bool is_primary)
1197{
1198 enum drm_plane_type type;
1199
1200 type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
1201 return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
Ville Syrjäläb0b3b792015-12-09 16:19:55 +02001202 formats, format_count, type, NULL);
Matt Roperdc415ff2014-04-01 15:22:36 -07001203}
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001204EXPORT_SYMBOL(drm_plane_init);
1205
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001206/**
1207 * drm_plane_cleanup - Clean up the core plane usage
1208 * @plane: plane to cleanup
1209 *
1210 * This function cleans up @plane and removes it from the DRM mode setting
1211 * core. Note that the function does *not* free the plane structure itself,
1212 * this is the responsibility of the caller.
1213 */
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001214void drm_plane_cleanup(struct drm_plane *plane)
1215{
1216 struct drm_device *dev = plane->dev;
1217
Daniel Vetter84849902012-12-02 00:28:11 +01001218 drm_modeset_lock_all(dev);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001219 kfree(plane->format_types);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10001220 drm_mode_object_unregister(dev, &plane->base);
Matt Roperdc415ff2014-04-01 15:22:36 -07001221
1222 BUG_ON(list_empty(&plane->head));
1223
Chris Wilson490d3d12016-05-27 20:05:00 +01001224 /* Note that the plane_list is considered to be static; should we
1225 * remove the drm_plane at runtime we would have to decrement all
1226 * the indices on the drm_plane after us in the plane_list.
1227 */
1228
Matt Roperdc415ff2014-04-01 15:22:36 -07001229 list_del(&plane->head);
1230 dev->mode_config.num_total_plane--;
1231 if (plane->type == DRM_PLANE_TYPE_OVERLAY)
1232 dev->mode_config.num_overlay_plane--;
Daniel Vetter84849902012-12-02 00:28:11 +01001233 drm_modeset_unlock_all(dev);
Thierry Reding3009c032014-11-25 12:09:49 +01001234
1235 WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
1236 if (plane->state && plane->funcs->atomic_destroy_state)
1237 plane->funcs->atomic_destroy_state(plane, plane->state);
Thierry Redinga18c0af2014-12-10 11:38:49 +01001238
Ville Syrjälä9f4c97a2015-12-08 18:41:54 +02001239 kfree(plane->name);
1240
Thierry Redinga18c0af2014-12-10 11:38:49 +01001241 memset(plane, 0, sizeof(*plane));
Jesse Barnes8cf5c912011-11-14 14:51:27 -08001242}
1243EXPORT_SYMBOL(drm_plane_cleanup);
1244
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001245/**
Chandra Konduruf81338a2015-04-09 17:36:21 -07001246 * drm_plane_from_index - find the registered plane at an index
1247 * @dev: DRM device
1248 * @idx: index of registered plane to find for
1249 *
1250 * Given a plane index, return the registered plane from DRM device's
1251 * list of planes with matching index.
1252 */
1253struct drm_plane *
1254drm_plane_from_index(struct drm_device *dev, int idx)
1255{
1256 struct drm_plane *plane;
Chandra Konduruf81338a2015-04-09 17:36:21 -07001257
Chris Wilson490d3d12016-05-27 20:05:00 +01001258 drm_for_each_plane(plane, dev)
1259 if (idx == plane->index)
Chandra Konduruf81338a2015-04-09 17:36:21 -07001260 return plane;
Chris Wilson490d3d12016-05-27 20:05:00 +01001261
Chandra Konduruf81338a2015-04-09 17:36:21 -07001262 return NULL;
1263}
1264EXPORT_SYMBOL(drm_plane_from_index);
1265
1266/**
Ville Syrjälä35f2c3a2013-06-05 15:39:56 +03001267 * drm_plane_force_disable - Forcibly disable a plane
1268 * @plane: plane to disable
1269 *
1270 * Forces the plane to be disabled.
1271 *
1272 * Used when the plane's current framebuffer is destroyed,
1273 * and when restoring fbdev mode.
1274 */
Ville Syrjälä9125e612013-06-03 16:10:40 +03001275void drm_plane_force_disable(struct drm_plane *plane)
1276{
1277 int ret;
1278
Daniel Vetter3d30a592014-07-27 13:42:42 +02001279 if (!plane->fb)
Ville Syrjälä9125e612013-06-03 16:10:40 +03001280 return;
1281
Daniel Vetter3d30a592014-07-27 13:42:42 +02001282 plane->old_fb = plane->fb;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001283 ret = plane->funcs->disable_plane(plane);
Daniel Vetter731cce42014-04-23 10:24:11 +02001284 if (ret) {
Ville Syrjälä9125e612013-06-03 16:10:40 +03001285 DRM_ERROR("failed to disable plane with busy fb\n");
Daniel Vetter3d30a592014-07-27 13:42:42 +02001286 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02001287 return;
1288 }
Ville Syrjälä9125e612013-06-03 16:10:40 +03001289 /* disconnect the plane from the fb and crtc: */
Daniel Vetter220dd2b2015-02-27 12:58:13 +01001290 drm_framebuffer_unreference(plane->old_fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02001291 plane->old_fb = NULL;
Ville Syrjälä9125e612013-06-03 16:10:40 +03001292 plane->fb = NULL;
1293 plane->crtc = NULL;
1294}
1295EXPORT_SYMBOL(drm_plane_force_disable);
1296
Benjamin Gaignard79190ea2016-06-21 16:37:09 +02001297int drm_modeset_register_all(struct drm_device *dev)
1298{
1299 int ret;
1300
1301 ret = drm_plane_register_all(dev);
1302 if (ret)
1303 goto err_plane;
1304
1305 ret = drm_crtc_register_all(dev);
1306 if (ret)
1307 goto err_crtc;
1308
1309 ret = drm_encoder_register_all(dev);
1310 if (ret)
1311 goto err_encoder;
1312
1313 ret = drm_connector_register_all(dev);
1314 if (ret)
1315 goto err_connector;
1316
1317 return 0;
1318
1319err_connector:
1320 drm_encoder_unregister_all(dev);
1321err_encoder:
1322 drm_crtc_unregister_all(dev);
1323err_crtc:
1324 drm_plane_unregister_all(dev);
1325err_plane:
1326 return ret;
1327}
1328
1329void drm_modeset_unregister_all(struct drm_device *dev)
1330{
1331 drm_connector_unregister_all(dev);
1332 drm_encoder_unregister_all(dev);
1333 drm_crtc_unregister_all(dev);
1334 drm_plane_unregister_all(dev);
1335}
1336
Rob Clark6b4959f2014-12-18 16:01:53 -05001337static int drm_mode_create_standard_properties(struct drm_device *dev)
Dave Airlief453ba02008-11-07 14:05:41 -08001338{
Rob Clark356af0e2014-12-18 16:01:52 -05001339 struct drm_property *prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001340
1341 /*
1342 * Standard properties (apply to all connectors)
1343 */
Rob Clark356af0e2014-12-18 16:01:52 -05001344 prop = drm_property_create(dev, DRM_MODE_PROP_BLOB |
Dave Airlief453ba02008-11-07 14:05:41 -08001345 DRM_MODE_PROP_IMMUTABLE,
1346 "EDID", 0);
Rob Clark356af0e2014-12-18 16:01:52 -05001347 if (!prop)
1348 return -ENOMEM;
1349 dev->mode_config.edid_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001350
Rob Clark356af0e2014-12-18 16:01:52 -05001351 prop = drm_property_create_enum(dev, 0,
Sascha Hauer4a67d392012-02-06 10:58:17 +01001352 "DPMS", drm_dpms_enum_list,
1353 ARRAY_SIZE(drm_dpms_enum_list));
Rob Clark356af0e2014-12-18 16:01:52 -05001354 if (!prop)
1355 return -ENOMEM;
1356 dev->mode_config.dpms_property = prop;
Dave Airlief453ba02008-11-07 14:05:41 -08001357
Rob Clark356af0e2014-12-18 16:01:52 -05001358 prop = drm_property_create(dev,
1359 DRM_MODE_PROP_BLOB |
1360 DRM_MODE_PROP_IMMUTABLE,
1361 "PATH", 0);
1362 if (!prop)
1363 return -ENOMEM;
1364 dev->mode_config.path_property = prop;
Dave Airlie43aba7e2014-06-05 14:01:31 +10001365
Rob Clark356af0e2014-12-18 16:01:52 -05001366 prop = drm_property_create(dev,
1367 DRM_MODE_PROP_BLOB |
1368 DRM_MODE_PROP_IMMUTABLE,
1369 "TILE", 0);
1370 if (!prop)
1371 return -ENOMEM;
1372 dev->mode_config.tile_property = prop;
Dave Airlie6f134d72014-10-20 16:30:50 +10001373
Rob Clark6b4959f2014-12-18 16:01:53 -05001374 prop = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Rob Clark9922ab52014-04-01 20:16:57 -04001375 "type", drm_plane_type_enum_list,
1376 ARRAY_SIZE(drm_plane_type_enum_list));
Rob Clark6b4959f2014-12-18 16:01:53 -05001377 if (!prop)
1378 return -ENOMEM;
1379 dev->mode_config.plane_type_property = prop;
1380
1381 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1382 "SRC_X", 0, UINT_MAX);
1383 if (!prop)
1384 return -ENOMEM;
1385 dev->mode_config.prop_src_x = prop;
1386
1387 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1388 "SRC_Y", 0, UINT_MAX);
1389 if (!prop)
1390 return -ENOMEM;
1391 dev->mode_config.prop_src_y = prop;
1392
1393 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1394 "SRC_W", 0, UINT_MAX);
1395 if (!prop)
1396 return -ENOMEM;
1397 dev->mode_config.prop_src_w = prop;
1398
1399 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1400 "SRC_H", 0, UINT_MAX);
1401 if (!prop)
1402 return -ENOMEM;
1403 dev->mode_config.prop_src_h = prop;
1404
1405 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1406 "CRTC_X", INT_MIN, INT_MAX);
1407 if (!prop)
1408 return -ENOMEM;
1409 dev->mode_config.prop_crtc_x = prop;
1410
1411 prop = drm_property_create_signed_range(dev, DRM_MODE_PROP_ATOMIC,
1412 "CRTC_Y", INT_MIN, INT_MAX);
1413 if (!prop)
1414 return -ENOMEM;
1415 dev->mode_config.prop_crtc_y = prop;
1416
1417 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1418 "CRTC_W", 0, INT_MAX);
1419 if (!prop)
1420 return -ENOMEM;
1421 dev->mode_config.prop_crtc_w = prop;
1422
1423 prop = drm_property_create_range(dev, DRM_MODE_PROP_ATOMIC,
1424 "CRTC_H", 0, INT_MAX);
1425 if (!prop)
1426 return -ENOMEM;
1427 dev->mode_config.prop_crtc_h = prop;
1428
1429 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1430 "FB_ID", DRM_MODE_OBJECT_FB);
1431 if (!prop)
1432 return -ENOMEM;
1433 dev->mode_config.prop_fb_id = prop;
1434
1435 prop = drm_property_create_object(dev, DRM_MODE_PROP_ATOMIC,
1436 "CRTC_ID", DRM_MODE_OBJECT_CRTC);
1437 if (!prop)
1438 return -ENOMEM;
1439 dev->mode_config.prop_crtc_id = prop;
Rob Clark9922ab52014-04-01 20:16:57 -04001440
Daniel Vettereab3bbe2015-01-22 16:36:21 +01001441 prop = drm_property_create_bool(dev, DRM_MODE_PROP_ATOMIC,
1442 "ACTIVE");
1443 if (!prop)
1444 return -ENOMEM;
1445 dev->mode_config.prop_active = prop;
1446
Daniel Stone955f3c32015-05-25 19:11:52 +01001447 prop = drm_property_create(dev,
1448 DRM_MODE_PROP_ATOMIC | DRM_MODE_PROP_BLOB,
1449 "MODE_ID", 0);
1450 if (!prop)
1451 return -ENOMEM;
1452 dev->mode_config.prop_mode_id = prop;
1453
Lionel Landwerlin5488dc12016-02-26 17:05:00 +00001454 prop = drm_property_create(dev,
1455 DRM_MODE_PROP_BLOB,
1456 "DEGAMMA_LUT", 0);
1457 if (!prop)
1458 return -ENOMEM;
1459 dev->mode_config.degamma_lut_property = prop;
1460
1461 prop = drm_property_create_range(dev,
1462 DRM_MODE_PROP_IMMUTABLE,
1463 "DEGAMMA_LUT_SIZE", 0, UINT_MAX);
1464 if (!prop)
1465 return -ENOMEM;
1466 dev->mode_config.degamma_lut_size_property = prop;
1467
1468 prop = drm_property_create(dev,
1469 DRM_MODE_PROP_BLOB,
1470 "CTM", 0);
1471 if (!prop)
1472 return -ENOMEM;
1473 dev->mode_config.ctm_property = prop;
1474
1475 prop = drm_property_create(dev,
1476 DRM_MODE_PROP_BLOB,
1477 "GAMMA_LUT", 0);
1478 if (!prop)
1479 return -ENOMEM;
1480 dev->mode_config.gamma_lut_property = prop;
1481
1482 prop = drm_property_create_range(dev,
1483 DRM_MODE_PROP_IMMUTABLE,
1484 "GAMMA_LUT_SIZE", 0, UINT_MAX);
1485 if (!prop)
1486 return -ENOMEM;
1487 dev->mode_config.gamma_lut_size_property = prop;
1488
Rob Clark9922ab52014-04-01 20:16:57 -04001489 return 0;
1490}
1491
Dave Airlief453ba02008-11-07 14:05:41 -08001492/**
1493 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
1494 * @dev: DRM device
1495 *
1496 * Called by a driver the first time a DVI-I connector is made.
1497 */
1498int drm_mode_create_dvi_i_properties(struct drm_device *dev)
1499{
1500 struct drm_property *dvi_i_selector;
1501 struct drm_property *dvi_i_subconnector;
Dave Airlief453ba02008-11-07 14:05:41 -08001502
1503 if (dev->mode_config.dvi_i_select_subconnector_property)
1504 return 0;
1505
1506 dvi_i_selector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001507 drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001508 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001509 drm_dvi_i_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001510 ARRAY_SIZE(drm_dvi_i_select_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001511 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
1512
Sascha Hauer4a67d392012-02-06 10:58:17 +01001513 dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
Dave Airlief453ba02008-11-07 14:05:41 -08001514 "subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001515 drm_dvi_i_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001516 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001517 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
1518
1519 return 0;
1520}
1521EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
1522
1523/**
1524 * drm_create_tv_properties - create TV specific connector properties
1525 * @dev: DRM device
1526 * @num_modes: number of different TV formats (modes) supported
1527 * @modes: array of pointers to strings containing name of each format
1528 *
1529 * Called by a driver's TV initialization routine, this function creates
1530 * the TV specific connector properties for a given device. Caller is
1531 * responsible for allocating a list of format names and passing them to
1532 * this routine.
1533 */
Thierry Reding2f763312014-10-13 12:45:57 +02001534int drm_mode_create_tv_properties(struct drm_device *dev,
1535 unsigned int num_modes,
Ville Syrjäläb7c914b2015-08-31 15:09:26 +03001536 const char * const modes[])
Dave Airlief453ba02008-11-07 14:05:41 -08001537{
1538 struct drm_property *tv_selector;
1539 struct drm_property *tv_subconnector;
Thierry Reding2f763312014-10-13 12:45:57 +02001540 unsigned int i;
Dave Airlief453ba02008-11-07 14:05:41 -08001541
1542 if (dev->mode_config.tv_select_subconnector_property)
1543 return 0;
1544
1545 /*
1546 * Basic connector properties
1547 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01001548 tv_selector = drm_property_create_enum(dev, 0,
Dave Airlief453ba02008-11-07 14:05:41 -08001549 "select subconnector",
Sascha Hauer4a67d392012-02-06 10:58:17 +01001550 drm_tv_select_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001551 ARRAY_SIZE(drm_tv_select_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001552 if (!tv_selector)
1553 goto nomem;
1554
Dave Airlief453ba02008-11-07 14:05:41 -08001555 dev->mode_config.tv_select_subconnector_property = tv_selector;
1556
1557 tv_subconnector =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001558 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
1559 "subconnector",
1560 drm_tv_subconnector_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001561 ARRAY_SIZE(drm_tv_subconnector_enum_list));
Insu Yun48aa1e72015-10-19 16:33:30 +00001562 if (!tv_subconnector)
1563 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001564 dev->mode_config.tv_subconnector_property = tv_subconnector;
1565
1566 /*
1567 * Other, TV specific properties: margins & TV modes.
1568 */
1569 dev->mode_config.tv_left_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001570 drm_property_create_range(dev, 0, "left margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001571 if (!dev->mode_config.tv_left_margin_property)
1572 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001573
1574 dev->mode_config.tv_right_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001575 drm_property_create_range(dev, 0, "right margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001576 if (!dev->mode_config.tv_right_margin_property)
1577 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001578
1579 dev->mode_config.tv_top_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001580 drm_property_create_range(dev, 0, "top margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001581 if (!dev->mode_config.tv_top_margin_property)
1582 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001583
1584 dev->mode_config.tv_bottom_margin_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001585 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001586 if (!dev->mode_config.tv_bottom_margin_property)
1587 goto nomem;
Dave Airlief453ba02008-11-07 14:05:41 -08001588
1589 dev->mode_config.tv_mode_property =
1590 drm_property_create(dev, DRM_MODE_PROP_ENUM,
1591 "mode", num_modes);
Insu Yun48aa1e72015-10-19 16:33:30 +00001592 if (!dev->mode_config.tv_mode_property)
1593 goto nomem;
1594
Dave Airlief453ba02008-11-07 14:05:41 -08001595 for (i = 0; i < num_modes; i++)
1596 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
1597 i, modes[i]);
1598
Francisco Jerezb6b79022009-08-02 04:19:20 +02001599 dev->mode_config.tv_brightness_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001600 drm_property_create_range(dev, 0, "brightness", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001601 if (!dev->mode_config.tv_brightness_property)
1602 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001603
1604 dev->mode_config.tv_contrast_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001605 drm_property_create_range(dev, 0, "contrast", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001606 if (!dev->mode_config.tv_contrast_property)
1607 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001608
1609 dev->mode_config.tv_flicker_reduction_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001610 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001611 if (!dev->mode_config.tv_flicker_reduction_property)
1612 goto nomem;
Francisco Jerezb6b79022009-08-02 04:19:20 +02001613
Francisco Jereza75f0232009-08-12 02:30:10 +02001614 dev->mode_config.tv_overscan_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001615 drm_property_create_range(dev, 0, "overscan", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001616 if (!dev->mode_config.tv_overscan_property)
1617 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001618
1619 dev->mode_config.tv_saturation_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001620 drm_property_create_range(dev, 0, "saturation", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001621 if (!dev->mode_config.tv_saturation_property)
1622 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001623
1624 dev->mode_config.tv_hue_property =
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01001625 drm_property_create_range(dev, 0, "hue", 0, 100);
Insu Yun48aa1e72015-10-19 16:33:30 +00001626 if (!dev->mode_config.tv_hue_property)
1627 goto nomem;
Francisco Jereza75f0232009-08-12 02:30:10 +02001628
Dave Airlief453ba02008-11-07 14:05:41 -08001629 return 0;
Insu Yun48aa1e72015-10-19 16:33:30 +00001630nomem:
1631 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08001632}
1633EXPORT_SYMBOL(drm_mode_create_tv_properties);
1634
1635/**
1636 * drm_mode_create_scaling_mode_property - create scaling mode property
1637 * @dev: DRM device
1638 *
1639 * Called by a driver the first time it's needed, must be attached to desired
1640 * connectors.
1641 */
1642int drm_mode_create_scaling_mode_property(struct drm_device *dev)
1643{
1644 struct drm_property *scaling_mode;
Dave Airlief453ba02008-11-07 14:05:41 -08001645
1646 if (dev->mode_config.scaling_mode_property)
1647 return 0;
1648
1649 scaling_mode =
Sascha Hauer4a67d392012-02-06 10:58:17 +01001650 drm_property_create_enum(dev, 0, "scaling mode",
1651 drm_scaling_mode_enum_list,
Dave Airlief453ba02008-11-07 14:05:41 -08001652 ARRAY_SIZE(drm_scaling_mode_enum_list));
Dave Airlief453ba02008-11-07 14:05:41 -08001653
1654 dev->mode_config.scaling_mode_property = scaling_mode;
1655
1656 return 0;
1657}
1658EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
1659
1660/**
Vandana Kannanff587e42014-06-11 10:46:48 +05301661 * drm_mode_create_aspect_ratio_property - create aspect ratio property
1662 * @dev: DRM device
1663 *
1664 * Called by a driver the first time it's needed, must be attached to desired
1665 * connectors.
1666 *
1667 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001668 * Zero on success, negative errno on failure.
Vandana Kannanff587e42014-06-11 10:46:48 +05301669 */
1670int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
1671{
1672 if (dev->mode_config.aspect_ratio_property)
1673 return 0;
1674
1675 dev->mode_config.aspect_ratio_property =
1676 drm_property_create_enum(dev, 0, "aspect ratio",
1677 drm_aspect_ratio_enum_list,
1678 ARRAY_SIZE(drm_aspect_ratio_enum_list));
1679
1680 if (dev->mode_config.aspect_ratio_property == NULL)
1681 return -ENOMEM;
1682
1683 return 0;
1684}
1685EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
1686
1687/**
Dave Airlie5bb2bbf2014-11-10 10:18:15 +10001688 * drm_mode_create_suggested_offset_properties - create suggests offset properties
1689 * @dev: DRM device
1690 *
1691 * Create the the suggested x/y offset property for connectors.
1692 */
1693int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
1694{
1695 if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
1696 return 0;
1697
1698 dev->mode_config.suggested_x_property =
1699 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
1700
1701 dev->mode_config.suggested_y_property =
1702 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
1703
1704 if (dev->mode_config.suggested_x_property == NULL ||
1705 dev->mode_config.suggested_y_property == NULL)
1706 return -ENOMEM;
1707 return 0;
1708}
1709EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
1710
Dave Airlief453ba02008-11-07 14:05:41 -08001711/**
Dave Airlief453ba02008-11-07 14:05:41 -08001712 * drm_mode_getresources - get graphics configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001713 * @dev: drm device for the ioctl
1714 * @data: data pointer for the ioctl
1715 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001716 *
Dave Airlief453ba02008-11-07 14:05:41 -08001717 * Construct a set of configuration description structures and return
1718 * them to the user, including CRTC, connector and framebuffer configuration.
1719 *
1720 * Called by the user via ioctl.
1721 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001722 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001723 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001724 */
1725int drm_mode_getresources(struct drm_device *dev, void *data,
1726 struct drm_file *file_priv)
1727{
1728 struct drm_mode_card_res *card_res = data;
1729 struct list_head *lh;
1730 struct drm_framebuffer *fb;
1731 struct drm_connector *connector;
1732 struct drm_crtc *crtc;
1733 struct drm_encoder *encoder;
1734 int ret = 0;
1735 int connector_count = 0;
1736 int crtc_count = 0;
1737 int fb_count = 0;
1738 int encoder_count = 0;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001739 int copied = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001740 uint32_t __user *fb_id;
1741 uint32_t __user *crtc_id;
1742 uint32_t __user *connector_id;
1743 uint32_t __user *encoder_id;
Dave Airlief453ba02008-11-07 14:05:41 -08001744
Dave Airliefb3b06c2011-02-08 13:55:21 +10001745 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1746 return -EINVAL;
1747
Dave Airlief453ba02008-11-07 14:05:41 -08001748
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001749 mutex_lock(&file_priv->fbs_lock);
Dave Airlief453ba02008-11-07 14:05:41 -08001750 /*
1751 * For the non-control nodes we need to limit the list of resources
1752 * by IDs in the group list for this node
1753 */
1754 list_for_each(lh, &file_priv->fbs)
1755 fb_count++;
1756
Daniel Vetter4b096ac2012-12-10 21:19:18 +01001757 /* handle this in 4 parts */
1758 /* FBs */
1759 if (card_res->count_fbs >= fb_count) {
1760 copied = 0;
1761 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1762 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1763 if (put_user(fb->base.id, fb_id + copied)) {
1764 mutex_unlock(&file_priv->fbs_lock);
1765 return -EFAULT;
1766 }
1767 copied++;
1768 }
1769 }
1770 card_res->count_fbs = fb_count;
1771 mutex_unlock(&file_priv->fbs_lock);
1772
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001773 /* mode_config.mutex protects the connector list against e.g. DP MST
1774 * connector hot-adding. CRTC/Plane lists are invariant. */
1775 mutex_lock(&dev->mode_config.mutex);
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001776 drm_for_each_crtc(crtc, dev)
1777 crtc_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001778
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001779 drm_for_each_connector(connector, dev)
1780 connector_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001781
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001782 drm_for_each_encoder(encoder, dev)
1783 encoder_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08001784
1785 card_res->max_height = dev->mode_config.max_height;
1786 card_res->min_height = dev->mode_config.min_height;
1787 card_res->max_width = dev->mode_config.max_width;
1788 card_res->min_width = dev->mode_config.min_width;
1789
Dave Airlief453ba02008-11-07 14:05:41 -08001790 /* CRTCs */
1791 if (card_res->count_crtcs >= crtc_count) {
1792 copied = 0;
1793 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001794 drm_for_each_crtc(crtc, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001795 if (put_user(crtc->base.id, crtc_id + copied)) {
1796 ret = -EFAULT;
1797 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001798 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001799 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001800 }
1801 }
1802 card_res->count_crtcs = crtc_count;
1803
1804 /* Encoders */
1805 if (card_res->count_encoders >= encoder_count) {
1806 copied = 0;
1807 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001808 drm_for_each_encoder(encoder, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001809 if (put_user(encoder->base.id, encoder_id +
1810 copied)) {
1811 ret = -EFAULT;
1812 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001813 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001814 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001815 }
1816 }
1817 card_res->count_encoders = encoder_count;
1818
1819 /* Connectors */
1820 if (card_res->count_connectors >= connector_count) {
1821 copied = 0;
1822 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001823 drm_for_each_connector(connector, dev) {
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001824 if (put_user(connector->base.id,
1825 connector_id + copied)) {
1826 ret = -EFAULT;
1827 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08001828 }
Daniel Vetter9c7060f2015-07-09 23:44:36 +02001829 copied++;
Dave Airlief453ba02008-11-07 14:05:41 -08001830 }
1831 }
1832 card_res->count_connectors = connector_count;
1833
Dave Airlief453ba02008-11-07 14:05:41 -08001834out:
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001835 mutex_unlock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001836 return ret;
1837}
1838
1839/**
1840 * drm_mode_getcrtc - get CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001841 * @dev: drm device for the ioctl
1842 * @data: data pointer for the ioctl
1843 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001844 *
Dave Airlief453ba02008-11-07 14:05:41 -08001845 * Construct a CRTC configuration structure to return to the user.
1846 *
1847 * Called by the user via ioctl.
1848 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001849 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001850 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001851 */
1852int drm_mode_getcrtc(struct drm_device *dev,
1853 void *data, struct drm_file *file_priv)
1854{
1855 struct drm_mode_crtc *crtc_resp = data;
1856 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08001857
Dave Airliefb3b06c2011-02-08 13:55:21 +10001858 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1859 return -EINVAL;
1860
Rob Clarka2b34e22013-10-05 16:36:52 -04001861 crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001862 if (!crtc)
1863 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08001864
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001865 drm_modeset_lock_crtc(crtc, crtc->primary);
Dave Airlief453ba02008-11-07 14:05:41 -08001866 crtc_resp->gamma_size = crtc->gamma_size;
Matt Roperf4510a22014-04-01 15:22:40 -07001867 if (crtc->primary->fb)
1868 crtc_resp->fb_id = crtc->primary->fb->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08001869 else
1870 crtc_resp->fb_id = 0;
1871
Daniel Vetter31c946e2015-02-22 12:24:17 +01001872 if (crtc->state) {
1873 crtc_resp->x = crtc->primary->state->src_x >> 16;
1874 crtc_resp->y = crtc->primary->state->src_y >> 16;
1875 if (crtc->state->enable) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001876 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->state->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001877 crtc_resp->mode_valid = 1;
Dave Airlief453ba02008-11-07 14:05:41 -08001878
Daniel Vetter31c946e2015-02-22 12:24:17 +01001879 } else {
1880 crtc_resp->mode_valid = 0;
1881 }
Dave Airlief453ba02008-11-07 14:05:41 -08001882 } else {
Daniel Vetter31c946e2015-02-22 12:24:17 +01001883 crtc_resp->x = crtc->x;
1884 crtc_resp->y = crtc->y;
1885 if (crtc->enabled) {
Daniel Stone934a8a82015-05-22 13:34:48 +01001886 drm_mode_convert_to_umode(&crtc_resp->mode, &crtc->mode);
Daniel Vetter31c946e2015-02-22 12:24:17 +01001887 crtc_resp->mode_valid = 1;
1888
1889 } else {
1890 crtc_resp->mode_valid = 0;
1891 }
Dave Airlief453ba02008-11-07 14:05:41 -08001892 }
Daniel Vetterfcf93f62014-11-12 08:45:01 +01001893 drm_modeset_unlock_crtc(crtc);
Dave Airlief453ba02008-11-07 14:05:41 -08001894
Daniel Vetterbaf698b2014-11-12 11:59:47 +01001895 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001896}
1897
Damien Lespiau61d8e322013-09-25 16:45:22 +01001898static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
1899 const struct drm_file *file_priv)
1900{
1901 /*
1902 * If user-space hasn't configured the driver to expose the stereo 3D
1903 * modes, don't expose them.
1904 */
1905 if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
1906 return false;
1907
1908 return true;
1909}
1910
Daniel Vetterabd69c52014-11-25 23:50:05 +01001911static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
1912{
1913 /* For atomic drivers only state objects are synchronously updated and
1914 * protected by modeset locks, so check those first. */
1915 if (connector->state)
1916 return connector->state->best_encoder;
1917 return connector->encoder;
1918}
1919
Rob Clark95cbf112014-12-18 16:01:49 -05001920/* helper for getconnector and getproperties ioctls */
Rob Clark88a48e22014-12-18 16:01:50 -05001921static int get_properties(struct drm_mode_object *obj, bool atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05001922 uint32_t __user *prop_ptr, uint64_t __user *prop_values,
1923 uint32_t *arg_count_props)
1924{
Rob Clark88a48e22014-12-18 16:01:50 -05001925 int props_count;
1926 int i, ret, copied;
1927
1928 props_count = obj->properties->count;
1929 if (!atomic)
1930 props_count -= obj->properties->atomic_count;
Rob Clark95cbf112014-12-18 16:01:49 -05001931
1932 if ((*arg_count_props >= props_count) && props_count) {
Rob Clark88a48e22014-12-18 16:01:50 -05001933 for (i = 0, copied = 0; copied < props_count; i++) {
Rob Clark95cbf112014-12-18 16:01:49 -05001934 struct drm_property *prop = obj->properties->properties[i];
1935 uint64_t val;
1936
Rob Clark88a48e22014-12-18 16:01:50 -05001937 if ((prop->flags & DRM_MODE_PROP_ATOMIC) && !atomic)
1938 continue;
1939
Rob Clark95cbf112014-12-18 16:01:49 -05001940 ret = drm_object_property_get_value(obj, prop, &val);
1941 if (ret)
1942 return ret;
1943
1944 if (put_user(prop->base.id, prop_ptr + copied))
1945 return -EFAULT;
1946
1947 if (put_user(val, prop_values + copied))
1948 return -EFAULT;
1949
1950 copied++;
1951 }
1952 }
1953 *arg_count_props = props_count;
1954
1955 return 0;
1956}
1957
Dave Airlief453ba02008-11-07 14:05:41 -08001958/**
1959 * drm_mode_getconnector - get connector configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01001960 * @dev: drm device for the ioctl
1961 * @data: data pointer for the ioctl
1962 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08001963 *
Dave Airlief453ba02008-11-07 14:05:41 -08001964 * Construct a connector configuration structure to return to the user.
1965 *
1966 * Called by the user via ioctl.
1967 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01001968 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01001969 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08001970 */
1971int drm_mode_getconnector(struct drm_device *dev, void *data,
1972 struct drm_file *file_priv)
1973{
1974 struct drm_mode_get_connector *out_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08001975 struct drm_connector *connector;
Daniel Vetterabd69c52014-11-25 23:50:05 +01001976 struct drm_encoder *encoder;
Dave Airlief453ba02008-11-07 14:05:41 -08001977 struct drm_display_mode *mode;
1978 int mode_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08001979 int encoders_count = 0;
1980 int ret = 0;
1981 int copied = 0;
1982 int i;
1983 struct drm_mode_modeinfo u_mode;
1984 struct drm_mode_modeinfo __user *mode_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08001985 uint32_t __user *encoder_ptr;
1986
Dave Airliefb3b06c2011-02-08 13:55:21 +10001987 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1988 return -EINVAL;
1989
Dave Airlief453ba02008-11-07 14:05:41 -08001990 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1991
Daniel Vetter7b240562012-12-12 00:35:33 +01001992 mutex_lock(&dev->mode_config.mutex);
Dave Airlief453ba02008-11-07 14:05:41 -08001993
Dave Airlieb164d312016-04-27 11:10:09 +10001994 connector = drm_connector_lookup(dev, out_resp->connector_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04001995 if (!connector) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03001996 ret = -ENOENT;
Tommi Rantala04bdf442015-04-03 10:45:29 +03001997 goto out_unlock;
Dave Airlief453ba02008-11-07 14:05:41 -08001998 }
Dave Airlief453ba02008-11-07 14:05:41 -08001999
Thierry Reding01073b02014-12-10 13:03:38 +01002000 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++)
2001 if (connector->encoder_ids[i] != 0)
Dave Airlief453ba02008-11-07 14:05:41 -08002002 encoders_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002003
2004 if (out_resp->count_modes == 0) {
2005 connector->funcs->fill_modes(connector,
2006 dev->mode_config.max_width,
2007 dev->mode_config.max_height);
2008 }
2009
2010 /* delayed so we get modes regardless of pre-fill_modes state */
2011 list_for_each_entry(mode, &connector->modes, head)
Damien Lespiau61d8e322013-09-25 16:45:22 +01002012 if (drm_mode_expose_to_userspace(mode, file_priv))
2013 mode_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08002014
2015 out_resp->connector_id = connector->base.id;
2016 out_resp->connector_type = connector->connector_type;
2017 out_resp->connector_type_id = connector->connector_type_id;
2018 out_resp->mm_width = connector->display_info.width_mm;
2019 out_resp->mm_height = connector->display_info.height_mm;
2020 out_resp->subpixel = connector->display_info.subpixel_order;
2021 out_resp->connection = connector->status;
Daniel Vetter2caa80e2015-02-22 11:38:36 +01002022
2023 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002024 encoder = drm_connector_get_encoder(connector);
2025 if (encoder)
2026 out_resp->encoder_id = encoder->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002027 else
2028 out_resp->encoder_id = 0;
2029
2030 /*
2031 * This ioctl is called twice, once to determine how much space is
2032 * needed, and the 2nd time to fill it.
2033 */
2034 if ((out_resp->count_modes >= mode_count) && mode_count) {
2035 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002036 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002037 list_for_each_entry(mode, &connector->modes, head) {
Damien Lespiau61d8e322013-09-25 16:45:22 +01002038 if (!drm_mode_expose_to_userspace(mode, file_priv))
2039 continue;
2040
Daniel Stone934a8a82015-05-22 13:34:48 +01002041 drm_mode_convert_to_umode(&u_mode, mode);
Dave Airlief453ba02008-11-07 14:05:41 -08002042 if (copy_to_user(mode_ptr + copied,
2043 &u_mode, sizeof(u_mode))) {
2044 ret = -EFAULT;
2045 goto out;
2046 }
2047 copied++;
2048 }
2049 }
2050 out_resp->count_modes = mode_count;
2051
Rob Clark88a48e22014-12-18 16:01:50 -05002052 ret = get_properties(&connector->base, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05002053 (uint32_t __user *)(unsigned long)(out_resp->props_ptr),
2054 (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr),
2055 &out_resp->count_props);
2056 if (ret)
2057 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002058
2059 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
2060 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002061 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
Dave Airlief453ba02008-11-07 14:05:41 -08002062 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2063 if (connector->encoder_ids[i] != 0) {
2064 if (put_user(connector->encoder_ids[i],
2065 encoder_ptr + copied)) {
2066 ret = -EFAULT;
2067 goto out;
2068 }
2069 copied++;
2070 }
2071 }
2072 }
2073 out_resp->count_encoders = encoders_count;
2074
2075out:
Rob Clarkccfc0862014-12-18 16:01:48 -05002076 drm_modeset_unlock(&dev->mode_config.connection_mutex);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002077
Dave Airlieb164d312016-04-27 11:10:09 +10002078 drm_connector_unreference(connector);
Tommi Rantala04bdf442015-04-03 10:45:29 +03002079out_unlock:
Daniel Vetter7b240562012-12-12 00:35:33 +01002080 mutex_unlock(&dev->mode_config.mutex);
2081
Dave Airlief453ba02008-11-07 14:05:41 -08002082 return ret;
2083}
2084
Daniel Vetterabd69c52014-11-25 23:50:05 +01002085static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
2086{
2087 struct drm_connector *connector;
2088 struct drm_device *dev = encoder->dev;
2089 bool uses_atomic = false;
2090
2091 /* For atomic drivers only state objects are synchronously updated and
2092 * protected by modeset locks, so check those first. */
Daniel Vetter6295d602015-07-09 23:44:25 +02002093 drm_for_each_connector(connector, dev) {
Daniel Vetterabd69c52014-11-25 23:50:05 +01002094 if (!connector->state)
2095 continue;
2096
2097 uses_atomic = true;
2098
2099 if (connector->state->best_encoder != encoder)
2100 continue;
2101
2102 return connector->state->crtc;
2103 }
2104
2105 /* Don't return stale data (e.g. pending async disable). */
2106 if (uses_atomic)
2107 return NULL;
2108
2109 return encoder->crtc;
2110}
2111
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002112/**
2113 * drm_mode_getencoder - get encoder configuration
2114 * @dev: drm device for the ioctl
2115 * @data: data pointer for the ioctl
2116 * @file_priv: drm file for the ioctl call
2117 *
2118 * Construct a encoder configuration structure to return to the user.
2119 *
2120 * Called by the user via ioctl.
2121 *
2122 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002123 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002124 */
Dave Airlief453ba02008-11-07 14:05:41 -08002125int drm_mode_getencoder(struct drm_device *dev, void *data,
2126 struct drm_file *file_priv)
2127{
2128 struct drm_mode_get_encoder *enc_resp = data;
Dave Airlief453ba02008-11-07 14:05:41 -08002129 struct drm_encoder *encoder;
Daniel Vetterabd69c52014-11-25 23:50:05 +01002130 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002131
Dave Airliefb3b06c2011-02-08 13:55:21 +10002132 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2133 return -EINVAL;
2134
Rob Clarka2b34e22013-10-05 16:36:52 -04002135 encoder = drm_encoder_find(dev, enc_resp->encoder_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002136 if (!encoder)
2137 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002138
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002139 drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
Daniel Vetterabd69c52014-11-25 23:50:05 +01002140 crtc = drm_encoder_get_crtc(encoder);
2141 if (crtc)
2142 enc_resp->crtc_id = crtc->base.id;
Dave Airlief453ba02008-11-07 14:05:41 -08002143 else
2144 enc_resp->crtc_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002145 drm_modeset_unlock(&dev->mode_config.connection_mutex);
2146
Dave Airlief453ba02008-11-07 14:05:41 -08002147 enc_resp->encoder_type = encoder->encoder_type;
2148 enc_resp->encoder_id = encoder->base.id;
2149 enc_resp->possible_crtcs = encoder->possible_crtcs;
2150 enc_resp->possible_clones = encoder->possible_clones;
2151
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002152 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08002153}
2154
2155/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002156 * drm_mode_getplane_res - enumerate all plane resources
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002157 * @dev: DRM device
2158 * @data: ioctl data
2159 * @file_priv: DRM file info
2160 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002161 * Construct a list of plane ids to return to the user.
2162 *
2163 * Called by the user via ioctl.
2164 *
2165 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002166 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002167 */
2168int drm_mode_getplane_res(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002169 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002170{
2171 struct drm_mode_get_plane_res *plane_resp = data;
2172 struct drm_mode_config *config;
2173 struct drm_plane *plane;
2174 uint32_t __user *plane_ptr;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002175 int copied = 0;
Matt Roper681e7ec2014-04-01 15:22:42 -07002176 unsigned num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002177
2178 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2179 return -EINVAL;
2180
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002181 config = &dev->mode_config;
2182
Matt Roper681e7ec2014-04-01 15:22:42 -07002183 if (file_priv->universal_planes)
2184 num_planes = config->num_total_plane;
2185 else
2186 num_planes = config->num_overlay_plane;
2187
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002188 /*
2189 * This ioctl is called twice, once to determine how much space is
2190 * needed, and the 2nd time to fill it.
2191 */
Matt Roper681e7ec2014-04-01 15:22:42 -07002192 if (num_planes &&
2193 (plane_resp->count_planes >= num_planes)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002194 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002195
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002196 /* Plane lists are invariant, no locking needed. */
Daniel Vettere4f62542015-07-09 23:44:35 +02002197 drm_for_each_plane(plane, dev) {
Matt Roper681e7ec2014-04-01 15:22:42 -07002198 /*
2199 * Unless userspace set the 'universal planes'
2200 * capability bit, only advertise overlays.
2201 */
2202 if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
2203 !file_priv->universal_planes)
Matt Ropere27dde32014-04-01 15:22:30 -07002204 continue;
2205
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002206 if (put_user(plane->base.id, plane_ptr + copied))
2207 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002208 copied++;
2209 }
2210 }
Matt Roper681e7ec2014-04-01 15:22:42 -07002211 plane_resp->count_planes = num_planes;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002212
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002213 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002214}
2215
2216/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002217 * drm_mode_getplane - get plane configuration
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002218 * @dev: DRM device
2219 * @data: ioctl data
2220 * @file_priv: DRM file info
2221 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002222 * Construct a plane configuration structure to return to the user.
2223 *
2224 * Called by the user via ioctl.
2225 *
2226 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002227 * Zero on success, negative errno on failure.
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002228 */
2229int drm_mode_getplane(struct drm_device *dev, void *data,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002230 struct drm_file *file_priv)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002231{
2232 struct drm_mode_get_plane *plane_resp = data;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002233 struct drm_plane *plane;
2234 uint32_t __user *format_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002235
2236 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2237 return -EINVAL;
2238
Rob Clarka2b34e22013-10-05 16:36:52 -04002239 plane = drm_plane_find(dev, plane_resp->plane_id);
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002240 if (!plane)
2241 return -ENOENT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002242
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002243 drm_modeset_lock(&plane->mutex, NULL);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002244 if (plane->crtc)
2245 plane_resp->crtc_id = plane->crtc->base.id;
2246 else
2247 plane_resp->crtc_id = 0;
2248
2249 if (plane->fb)
2250 plane_resp->fb_id = plane->fb->base.id;
2251 else
2252 plane_resp->fb_id = 0;
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002253 drm_modeset_unlock(&plane->mutex);
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002254
2255 plane_resp->plane_id = plane->base.id;
2256 plane_resp->possible_crtcs = plane->possible_crtcs;
Ville Syrjälä778ad902013-06-03 16:11:42 +03002257 plane_resp->gamma_size = 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002258
2259 /*
2260 * This ioctl is called twice, once to determine how much space is
2261 * needed, and the 2nd time to fill it.
2262 */
2263 if (plane->format_count &&
2264 (plane_resp->count_format_types >= plane->format_count)) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002265 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002266 if (copy_to_user(format_ptr,
2267 plane->format_types,
2268 sizeof(uint32_t) * plane->format_count)) {
Daniel Vetterfcf93f62014-11-12 08:45:01 +01002269 return -EFAULT;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002270 }
2271 }
2272 plane_resp->count_format_types = plane->format_count;
2273
Daniel Vetterbaf698b2014-11-12 11:59:47 +01002274 return 0;
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002275}
2276
Laurent Pinchartead86102015-03-05 02:25:43 +02002277/**
2278 * drm_plane_check_pixel_format - Check if the plane supports the pixel format
2279 * @plane: plane to check for format support
2280 * @format: the pixel format
2281 *
2282 * Returns:
2283 * Zero of @plane has @format in its list of supported pixel formats, -EINVAL
2284 * otherwise.
2285 */
2286int drm_plane_check_pixel_format(const struct drm_plane *plane, u32 format)
2287{
2288 unsigned int i;
2289
2290 for (i = 0; i < plane->format_count; i++) {
2291 if (format == plane->format_types[i])
2292 return 0;
2293 }
2294
2295 return -EINVAL;
2296}
2297
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002298static int check_src_coords(uint32_t src_x, uint32_t src_y,
2299 uint32_t src_w, uint32_t src_h,
2300 const struct drm_framebuffer *fb)
2301{
2302 unsigned int fb_width, fb_height;
2303
2304 fb_width = fb->width << 16;
2305 fb_height = fb->height << 16;
2306
2307 /* Make sure source coordinates are inside the fb. */
2308 if (src_w > fb_width ||
2309 src_x > fb_width - src_w ||
2310 src_h > fb_height ||
2311 src_y > fb_height - src_h) {
2312 DRM_DEBUG_KMS("Invalid source coordinates "
2313 "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
2314 src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
2315 src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
2316 src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
2317 src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
2318 return -ENOSPC;
2319 }
2320
2321 return 0;
2322}
2323
Matt Roperb36552b2014-06-10 08:28:09 -07002324/*
2325 * setplane_internal - setplane handler for internal callers
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002326 *
Matt Roperb36552b2014-06-10 08:28:09 -07002327 * Note that we assume an extra reference has already been taken on fb. If the
2328 * update fails, this reference will be dropped before return; if it succeeds,
2329 * the previous framebuffer (if any) will be unreferenced instead.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002330 *
Matt Roperb36552b2014-06-10 08:28:09 -07002331 * src_{x,y,w,h} are provided in 16.16 fixed point format
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002332 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002333static int __setplane_internal(struct drm_plane *plane,
2334 struct drm_crtc *crtc,
2335 struct drm_framebuffer *fb,
2336 int32_t crtc_x, int32_t crtc_y,
2337 uint32_t crtc_w, uint32_t crtc_h,
2338 /* src_{x,y,w,h} values are 16.16 fixed point */
2339 uint32_t src_x, uint32_t src_y,
2340 uint32_t src_w, uint32_t src_h)
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002341{
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002342 int ret = 0;
2343
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002344 /* No fb means shut it down */
Matt Roperb36552b2014-06-10 08:28:09 -07002345 if (!fb) {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002346 plane->old_fb = plane->fb;
Daniel Vetter731cce42014-04-23 10:24:11 +02002347 ret = plane->funcs->disable_plane(plane);
2348 if (!ret) {
2349 plane->crtc = NULL;
2350 plane->fb = NULL;
2351 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002352 plane->old_fb = NULL;
Daniel Vetter731cce42014-04-23 10:24:11 +02002353 }
Jesse Barnes8cf5c912011-11-14 14:51:27 -08002354 goto out;
2355 }
2356
Matt Roper7f994f32014-05-29 08:06:51 -07002357 /* Check whether this plane is usable on this CRTC */
2358 if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
2359 DRM_DEBUG_KMS("Invalid crtc for plane\n");
2360 ret = -EINVAL;
2361 goto out;
2362 }
2363
Ville Syrjälä62443be2011-12-20 00:06:47 +02002364 /* Check whether this plane supports the fb pixel format. */
Laurent Pinchartead86102015-03-05 02:25:43 +02002365 ret = drm_plane_check_pixel_format(plane, fb->pixel_format);
2366 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01002367 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01002368 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
2369 kfree(format_name);
Ville Syrjälä62443be2011-12-20 00:06:47 +02002370 goto out;
2371 }
2372
Matt Roper3968be92015-04-13 11:06:13 -07002373 /* Give drivers some help against integer overflows */
2374 if (crtc_w > INT_MAX ||
2375 crtc_x > INT_MAX - (int32_t) crtc_w ||
2376 crtc_h > INT_MAX ||
2377 crtc_y > INT_MAX - (int32_t) crtc_h) {
2378 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
2379 crtc_w, crtc_h, crtc_x, crtc_y);
Ville Syrjäläc390eed2015-10-15 20:39:58 +03002380 ret = -ERANGE;
Ville Syrjälä42ef8782011-12-20 00:06:44 +02002381 goto out;
2382 }
2383
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002384 ret = check_src_coords(src_x, src_y, src_w, src_h, fb);
2385 if (ret)
Dave Airlief453ba02008-11-07 14:05:41 -08002386 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002387
Daniel Vetter3d30a592014-07-27 13:42:42 +02002388 plane->old_fb = plane->fb;
Dave Airlief453ba02008-11-07 14:05:41 -08002389 ret = plane->funcs->update_plane(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002390 crtc_x, crtc_y, crtc_w, crtc_h,
2391 src_x, src_y, src_w, src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002392 if (!ret) {
2393 plane->crtc = crtc;
2394 plane->fb = fb;
Daniel Vetter35f8bad2013-02-15 21:21:37 +01002395 fb = NULL;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002396 } else {
Daniel Vetter3d30a592014-07-27 13:42:42 +02002397 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002398 }
2399
2400out:
Daniel Vetter6c2a7532012-12-11 00:59:24 +01002401 if (fb)
2402 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002403 if (plane->old_fb)
2404 drm_framebuffer_unreference(plane->old_fb);
2405 plane->old_fb = NULL;
Dave Airlief453ba02008-11-07 14:05:41 -08002406
2407 return ret;
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002408}
Matt Roperb36552b2014-06-10 08:28:09 -07002409
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002410static int setplane_internal(struct drm_plane *plane,
2411 struct drm_crtc *crtc,
2412 struct drm_framebuffer *fb,
2413 int32_t crtc_x, int32_t crtc_y,
2414 uint32_t crtc_w, uint32_t crtc_h,
2415 /* src_{x,y,w,h} values are 16.16 fixed point */
2416 uint32_t src_x, uint32_t src_y,
2417 uint32_t src_w, uint32_t src_h)
2418{
2419 int ret;
2420
2421 drm_modeset_lock_all(plane->dev);
2422 ret = __setplane_internal(plane, crtc, fb,
2423 crtc_x, crtc_y, crtc_w, crtc_h,
2424 src_x, src_y, src_w, src_h);
2425 drm_modeset_unlock_all(plane->dev);
2426
2427 return ret;
Matt Roperb36552b2014-06-10 08:28:09 -07002428}
2429
2430/**
2431 * drm_mode_setplane - configure a plane's configuration
2432 * @dev: DRM device
2433 * @data: ioctl data*
2434 * @file_priv: DRM file info
2435 *
2436 * Set plane configuration, including placement, fb, scaling, and other factors.
2437 * Or pass a NULL fb to disable (planes may be disabled without providing a
2438 * valid crtc).
2439 *
2440 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002441 * Zero on success, negative errno on failure.
Matt Roperb36552b2014-06-10 08:28:09 -07002442 */
2443int drm_mode_setplane(struct drm_device *dev, void *data,
2444 struct drm_file *file_priv)
2445{
2446 struct drm_mode_set_plane *plane_req = data;
Matt Roperb36552b2014-06-10 08:28:09 -07002447 struct drm_plane *plane;
2448 struct drm_crtc *crtc = NULL;
2449 struct drm_framebuffer *fb = NULL;
2450
2451 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2452 return -EINVAL;
2453
Matt Roperb36552b2014-06-10 08:28:09 -07002454 /*
2455 * First, find the plane, crtc, and fb objects. If not available,
2456 * we don't bother to call the driver.
2457 */
Rob Clark933f6222014-11-25 20:33:11 -05002458 plane = drm_plane_find(dev, plane_req->plane_id);
2459 if (!plane) {
Matt Roperb36552b2014-06-10 08:28:09 -07002460 DRM_DEBUG_KMS("Unknown plane ID %d\n",
2461 plane_req->plane_id);
2462 return -ENOENT;
2463 }
Matt Roperb36552b2014-06-10 08:28:09 -07002464
2465 if (plane_req->fb_id) {
2466 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
2467 if (!fb) {
2468 DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
2469 plane_req->fb_id);
2470 return -ENOENT;
2471 }
2472
Rob Clark933f6222014-11-25 20:33:11 -05002473 crtc = drm_crtc_find(dev, plane_req->crtc_id);
2474 if (!crtc) {
Matt Roperb36552b2014-06-10 08:28:09 -07002475 DRM_DEBUG_KMS("Unknown crtc ID %d\n",
2476 plane_req->crtc_id);
2477 return -ENOENT;
2478 }
Matt Roperb36552b2014-06-10 08:28:09 -07002479 }
2480
Matt Roper161d0dc2014-06-10 08:28:10 -07002481 /*
2482 * setplane_internal will take care of deref'ing either the old or new
2483 * framebuffer depending on success.
2484 */
Chris Wilson17cfd912014-06-13 15:22:28 +01002485 return setplane_internal(plane, crtc, fb,
Matt Roperb36552b2014-06-10 08:28:09 -07002486 plane_req->crtc_x, plane_req->crtc_y,
2487 plane_req->crtc_w, plane_req->crtc_h,
2488 plane_req->src_x, plane_req->src_y,
2489 plane_req->src_w, plane_req->src_h);
Dave Airlief453ba02008-11-07 14:05:41 -08002490}
2491
2492/**
Daniel Vetter2d13b672012-12-11 13:47:23 +01002493 * drm_mode_set_config_internal - helper to call ->set_config
2494 * @set: modeset config to set
2495 *
2496 * This is a little helper to wrap internal calls to the ->set_config driver
2497 * interface. The only thing it adds is correct refcounting dance.
Thierry Reding4dfd9092014-12-10 13:03:34 +01002498 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002499 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002500 * Zero on success, negative errno on failure.
Daniel Vetter2d13b672012-12-11 13:47:23 +01002501 */
2502int drm_mode_set_config_internal(struct drm_mode_set *set)
2503{
2504 struct drm_crtc *crtc = set->crtc;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002505 struct drm_framebuffer *fb;
2506 struct drm_crtc *tmp;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002507 int ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002508
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002509 /*
2510 * NOTE: ->set_config can also disable other crtcs (if we steal all
2511 * connectors from it), hence we need to refcount the fbs across all
2512 * crtcs. Atomic modeset will have saner semantics ...
2513 */
Daniel Vettere4f62542015-07-09 23:44:35 +02002514 drm_for_each_crtc(tmp, crtc->dev)
Daniel Vetter3d30a592014-07-27 13:42:42 +02002515 tmp->primary->old_fb = tmp->primary->fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002516
Daniel Vetterb0d12322012-12-11 01:07:12 +01002517 fb = set->fb;
2518
2519 ret = crtc->funcs->set_config(set);
2520 if (ret == 0) {
Matt Ropere13161a2014-04-01 15:22:38 -07002521 crtc->primary->crtc = crtc;
Daniel Vetter0fe27f02014-04-23 17:34:06 +02002522 crtc->primary->fb = fb;
Daniel Vetter5cef29a2013-06-15 00:13:16 +02002523 }
Daniel Vettercc85e122013-06-15 00:13:15 +02002524
Daniel Vettere4f62542015-07-09 23:44:35 +02002525 drm_for_each_crtc(tmp, crtc->dev) {
Matt Roperf4510a22014-04-01 15:22:40 -07002526 if (tmp->primary->fb)
2527 drm_framebuffer_reference(tmp->primary->fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02002528 if (tmp->primary->old_fb)
2529 drm_framebuffer_unreference(tmp->primary->old_fb);
2530 tmp->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002531 }
2532
2533 return ret;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002534}
2535EXPORT_SYMBOL(drm_mode_set_config_internal);
2536
Matt Roperaf936292014-04-01 15:22:34 -07002537/**
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002538 * drm_crtc_get_hv_timing - Fetches hdisplay/vdisplay for given mode
2539 * @mode: mode to query
2540 * @hdisplay: hdisplay value to fill in
2541 * @vdisplay: vdisplay value to fill in
2542 *
2543 * The vdisplay value will be doubled if the specified mode is a stereo mode of
2544 * the appropriate layout.
2545 */
2546void drm_crtc_get_hv_timing(const struct drm_display_mode *mode,
2547 int *hdisplay, int *vdisplay)
2548{
2549 struct drm_display_mode adjusted;
2550
2551 drm_mode_copy(&adjusted, mode);
2552 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE_ONLY);
2553 *hdisplay = adjusted.crtc_hdisplay;
2554 *vdisplay = adjusted.crtc_vdisplay;
2555}
2556EXPORT_SYMBOL(drm_crtc_get_hv_timing);
2557
2558/**
Matt Roperaf936292014-04-01 15:22:34 -07002559 * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
2560 * CRTC viewport
2561 * @crtc: CRTC that framebuffer will be displayed on
2562 * @x: x panning
2563 * @y: y panning
2564 * @mode: mode that framebuffer will be displayed under
2565 * @fb: framebuffer to check size of
Damien Lespiauc11e9282013-09-25 16:45:30 +01002566 */
Matt Roperaf936292014-04-01 15:22:34 -07002567int drm_crtc_check_viewport(const struct drm_crtc *crtc,
2568 int x, int y,
2569 const struct drm_display_mode *mode,
2570 const struct drm_framebuffer *fb)
Damien Lespiauc11e9282013-09-25 16:45:30 +01002571
2572{
2573 int hdisplay, vdisplay;
2574
Gustavo Padovanecb7e162014-12-01 15:40:09 -08002575 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Damien Lespiaua0c1bbb2013-09-25 16:45:31 +01002576
Ville Syrjälä33e0be62015-10-16 18:38:39 +03002577 if (crtc->state &&
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03002578 crtc->primary->state->rotation & (DRM_ROTATE_90 |
2579 DRM_ROTATE_270))
Damien Lespiauc11e9282013-09-25 16:45:30 +01002580 swap(hdisplay, vdisplay);
2581
Ville Syrjäläce8d9ec2015-10-15 20:40:00 +03002582 return check_src_coords(x << 16, y << 16,
2583 hdisplay << 16, vdisplay << 16, fb);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002584}
Matt Roperaf936292014-04-01 15:22:34 -07002585EXPORT_SYMBOL(drm_crtc_check_viewport);
Damien Lespiauc11e9282013-09-25 16:45:30 +01002586
Daniel Vetter2d13b672012-12-11 13:47:23 +01002587/**
Dave Airlief453ba02008-11-07 14:05:41 -08002588 * drm_mode_setcrtc - set CRTC configuration
Daniel Vetter065a50ed2012-12-02 00:09:18 +01002589 * @dev: drm device for the ioctl
2590 * @data: data pointer for the ioctl
2591 * @file_priv: drm file for the ioctl call
Dave Airlief453ba02008-11-07 14:05:41 -08002592 *
Dave Airlief453ba02008-11-07 14:05:41 -08002593 * Build a new CRTC configuration based on user request.
2594 *
2595 * Called by the user via ioctl.
2596 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002597 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002598 * Zero on success, negative errno on failure.
Dave Airlief453ba02008-11-07 14:05:41 -08002599 */
2600int drm_mode_setcrtc(struct drm_device *dev, void *data,
2601 struct drm_file *file_priv)
2602{
2603 struct drm_mode_config *config = &dev->mode_config;
2604 struct drm_mode_crtc *crtc_req = data;
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002605 struct drm_crtc *crtc;
Dave Airlief453ba02008-11-07 14:05:41 -08002606 struct drm_connector **connector_set = NULL, *connector;
2607 struct drm_framebuffer *fb = NULL;
2608 struct drm_display_mode *mode = NULL;
2609 struct drm_mode_set set;
2610 uint32_t __user *set_connectors_ptr;
Laurent Pinchart4a1b0712012-05-17 13:27:21 +02002611 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08002612 int i;
2613
Dave Airliefb3b06c2011-02-08 13:55:21 +10002614 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2615 return -EINVAL;
2616
Zhao Junwang01447e92015-07-07 17:08:35 +08002617 /*
2618 * Universal plane src offsets are only 16.16, prevent havoc for
2619 * drivers using universal plane code internally.
2620 */
2621 if (crtc_req->x & 0xffff0000 || crtc_req->y & 0xffff0000)
Ville Syrjälä1d97e912012-03-13 12:35:41 +02002622 return -ERANGE;
2623
Daniel Vetter84849902012-12-02 00:28:11 +01002624 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04002625 crtc = drm_crtc_find(dev, crtc_req->crtc_id);
2626 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002627 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002628 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002629 goto out;
2630 }
Ville Syrjäläfa3ab4c2015-12-08 18:41:53 +02002631 DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002632
2633 if (crtc_req->mode_valid) {
2634 /* If we have a mode we need a framebuffer. */
2635 /* If we pass -1, set the mode with the currently bound fb */
2636 if (crtc_req->fb_id == -1) {
Matt Roperf4510a22014-04-01 15:22:40 -07002637 if (!crtc->primary->fb) {
Ville Syrjälä6653cc82012-03-13 12:35:38 +02002638 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
2639 ret = -EINVAL;
2640 goto out;
Dave Airlief453ba02008-11-07 14:05:41 -08002641 }
Matt Roperf4510a22014-04-01 15:22:40 -07002642 fb = crtc->primary->fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01002643 /* Make refcounting symmetric with the lookup path. */
2644 drm_framebuffer_reference(fb);
Dave Airlief453ba02008-11-07 14:05:41 -08002645 } else {
Daniel Vetter786b99e2012-12-02 21:53:40 +01002646 fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
2647 if (!fb) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002648 DRM_DEBUG_KMS("Unknown FB ID%d\n",
2649 crtc_req->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03002650 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002651 goto out;
2652 }
Dave Airlief453ba02008-11-07 14:05:41 -08002653 }
2654
2655 mode = drm_mode_create(dev);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002656 if (!mode) {
2657 ret = -ENOMEM;
2658 goto out;
2659 }
2660
Daniel Stone934a8a82015-05-22 13:34:48 +01002661 ret = drm_mode_convert_umode(mode, &crtc_req->mode);
Ville Syrjälä90367bf2012-03-13 12:35:44 +02002662 if (ret) {
2663 DRM_DEBUG_KMS("Invalid mode\n");
2664 goto out;
2665 }
2666
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002667 /*
2668 * Check whether the primary plane supports the fb pixel format.
2669 * Drivers not implementing the universal planes API use a
2670 * default formats list provided by the DRM core which doesn't
2671 * match real hardware capabilities. Skip the check in that
2672 * case.
2673 */
2674 if (!crtc->primary->format_default) {
2675 ret = drm_plane_check_pixel_format(crtc->primary,
2676 fb->pixel_format);
2677 if (ret) {
Eric Engestromd3828142016-08-15 16:29:55 +01002678 char *format_name = drm_get_format_name(fb->pixel_format);
Eric Engestrom90844f02016-08-15 01:02:38 +01002679 DRM_DEBUG_KMS("Invalid pixel format %s\n", format_name);
2680 kfree(format_name);
Laurent Pinchart7eb5f302015-03-09 10:41:07 +02002681 goto out;
2682 }
2683 }
2684
Damien Lespiauc11e9282013-09-25 16:45:30 +01002685 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
2686 mode, fb);
2687 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02002688 goto out;
Damien Lespiauc11e9282013-09-25 16:45:30 +01002689
Dave Airlief453ba02008-11-07 14:05:41 -08002690 }
2691
2692 if (crtc_req->count_connectors == 0 && mode) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002693 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
Dave Airlief453ba02008-11-07 14:05:41 -08002694 ret = -EINVAL;
2695 goto out;
2696 }
2697
Jakob Bornecrantz7781de72009-08-03 13:43:58 +01002698 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002699 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
Dave Airlief453ba02008-11-07 14:05:41 -08002700 crtc_req->count_connectors);
2701 ret = -EINVAL;
2702 goto out;
2703 }
2704
2705 if (crtc_req->count_connectors > 0) {
2706 u32 out_id;
2707
2708 /* Avoid unbounded kernel memory allocation */
2709 if (crtc_req->count_connectors > config->num_connector) {
2710 ret = -EINVAL;
2711 goto out;
2712 }
2713
Thierry Reding2f6c5382014-12-10 13:03:36 +01002714 connector_set = kmalloc_array(crtc_req->count_connectors,
2715 sizeof(struct drm_connector *),
2716 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08002717 if (!connector_set) {
2718 ret = -ENOMEM;
2719 goto out;
2720 }
2721
2722 for (i = 0; i < crtc_req->count_connectors; i++) {
Dave Airlieb164d312016-04-27 11:10:09 +10002723 connector_set[i] = NULL;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02002724 set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08002725 if (get_user(out_id, &set_connectors_ptr[i])) {
2726 ret = -EFAULT;
2727 goto out;
2728 }
2729
Dave Airlieb164d312016-04-27 11:10:09 +10002730 connector = drm_connector_lookup(dev, out_id);
Rob Clarka2b34e22013-10-05 16:36:52 -04002731 if (!connector) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002732 DRM_DEBUG_KMS("Connector id %d unknown\n",
2733 out_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002734 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002735 goto out;
2736 }
Jerome Glisse94401062010-07-15 15:43:25 -04002737 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
2738 connector->base.id,
Jani Nikula25933822014-06-03 14:56:20 +03002739 connector->name);
Dave Airlief453ba02008-11-07 14:05:41 -08002740
2741 connector_set[i] = connector;
2742 }
2743 }
2744
2745 set.crtc = crtc;
2746 set.x = crtc_req->x;
2747 set.y = crtc_req->y;
2748 set.mode = mode;
2749 set.connectors = connector_set;
2750 set.num_connectors = crtc_req->count_connectors;
Dave Airlie5ef5f722009-08-17 13:11:23 +10002751 set.fb = fb;
Daniel Vetter2d13b672012-12-11 13:47:23 +01002752 ret = drm_mode_set_config_internal(&set);
Dave Airlief453ba02008-11-07 14:05:41 -08002753
2754out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01002755 if (fb)
2756 drm_framebuffer_unreference(fb);
2757
Dave Airlieb164d312016-04-27 11:10:09 +10002758 if (connector_set) {
2759 for (i = 0; i < crtc_req->count_connectors; i++) {
2760 if (connector_set[i])
2761 drm_connector_unreference(connector_set[i]);
2762 }
2763 }
Dave Airlief453ba02008-11-07 14:05:41 -08002764 kfree(connector_set);
Ville Syrjäläee34ab52012-03-13 12:35:43 +02002765 drm_mode_destroy(dev, mode);
Daniel Vetter84849902012-12-02 00:28:11 +01002766 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08002767 return ret;
2768}
2769
Matt Roper161d0dc2014-06-10 08:28:10 -07002770/**
2771 * drm_mode_cursor_universal - translate legacy cursor ioctl call into a
2772 * universal plane handler call
2773 * @crtc: crtc to update cursor for
2774 * @req: data pointer for the ioctl
2775 * @file_priv: drm file for the ioctl call
2776 *
2777 * Legacy cursor ioctl's work directly with driver buffer handles. To
2778 * translate legacy ioctl calls into universal plane handler calls, we need to
2779 * wrap the native buffer handle in a drm_framebuffer.
2780 *
2781 * Note that we assume any handle passed to the legacy ioctls was a 32-bit ARGB
2782 * buffer with a pitch of 4*width; the universal plane interface should be used
2783 * directly in cases where the hardware can support other buffer settings and
2784 * userspace wants to make use of these capabilities.
2785 *
2786 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002787 * Zero on success, negative errno on failure.
Matt Roper161d0dc2014-06-10 08:28:10 -07002788 */
2789static int drm_mode_cursor_universal(struct drm_crtc *crtc,
2790 struct drm_mode_cursor2 *req,
2791 struct drm_file *file_priv)
2792{
2793 struct drm_device *dev = crtc->dev;
2794 struct drm_framebuffer *fb = NULL;
2795 struct drm_mode_fb_cmd2 fbreq = {
2796 .width = req->width,
2797 .height = req->height,
2798 .pixel_format = DRM_FORMAT_ARGB8888,
2799 .pitches = { req->width * 4 },
2800 .handles = { req->handle },
2801 };
2802 int32_t crtc_x, crtc_y;
2803 uint32_t crtc_w = 0, crtc_h = 0;
2804 uint32_t src_w = 0, src_h = 0;
2805 int ret = 0;
2806
2807 BUG_ON(!crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002808 WARN_ON(crtc->cursor->crtc != crtc && crtc->cursor->crtc != NULL);
Matt Roper161d0dc2014-06-10 08:28:10 -07002809
2810 /*
2811 * Obtain fb we'll be using (either new or existing) and take an extra
2812 * reference to it if fb != null. setplane will take care of dropping
2813 * the reference if the plane update fails.
2814 */
2815 if (req->flags & DRM_MODE_CURSOR_BO) {
2816 if (req->handle) {
Daniel Vetter7520a272016-08-15 16:07:02 +02002817 fb = drm_internal_framebuffer_create(dev, &fbreq, file_priv);
Matt Roper161d0dc2014-06-10 08:28:10 -07002818 if (IS_ERR(fb)) {
2819 DRM_DEBUG_KMS("failed to wrap cursor buffer in drm framebuffer\n");
2820 return PTR_ERR(fb);
2821 }
Gerd Hoffmanndd546592016-05-31 08:54:52 +02002822 fb->hot_x = req->hot_x;
2823 fb->hot_y = req->hot_y;
Matt Roper161d0dc2014-06-10 08:28:10 -07002824 } else {
2825 fb = NULL;
2826 }
2827 } else {
Matt Roper161d0dc2014-06-10 08:28:10 -07002828 fb = crtc->cursor->fb;
2829 if (fb)
2830 drm_framebuffer_reference(fb);
Matt Roper161d0dc2014-06-10 08:28:10 -07002831 }
2832
2833 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2834 crtc_x = req->x;
2835 crtc_y = req->y;
2836 } else {
2837 crtc_x = crtc->cursor_x;
2838 crtc_y = crtc->cursor_y;
2839 }
2840
2841 if (fb) {
2842 crtc_w = fb->width;
2843 crtc_h = fb->height;
2844 src_w = fb->width << 16;
2845 src_h = fb->height << 16;
2846 }
2847
2848 /*
2849 * setplane_internal will take care of deref'ing either the old or new
2850 * framebuffer depending on success.
2851 */
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002852 ret = __setplane_internal(crtc->cursor, crtc, fb,
Matt Roper161d0dc2014-06-10 08:28:10 -07002853 crtc_x, crtc_y, crtc_w, crtc_h,
2854 0, 0, src_w, src_h);
2855
2856 /* Update successful; save new cursor position, if necessary */
2857 if (ret == 0 && req->flags & DRM_MODE_CURSOR_MOVE) {
2858 crtc->cursor_x = req->x;
2859 crtc->cursor_y = req->y;
2860 }
2861
2862 return ret;
2863}
2864
Dave Airlie4c813d42013-06-20 11:48:52 +10002865static int drm_mode_cursor_common(struct drm_device *dev,
2866 struct drm_mode_cursor2 *req,
2867 struct drm_file *file_priv)
Dave Airlief453ba02008-11-07 14:05:41 -08002868{
Dave Airlief453ba02008-11-07 14:05:41 -08002869 struct drm_crtc *crtc;
2870 int ret = 0;
2871
Dave Airliefb3b06c2011-02-08 13:55:21 +10002872 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2873 return -EINVAL;
2874
Jakob Bornecrantz7c4eaca2012-08-16 08:29:03 +00002875 if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
Dave Airlief453ba02008-11-07 14:05:41 -08002876 return -EINVAL;
Dave Airlief453ba02008-11-07 14:05:41 -08002877
Rob Clarka2b34e22013-10-05 16:36:52 -04002878 crtc = drm_crtc_find(dev, req->crtc_id);
2879 if (!crtc) {
Zhao Yakui58367ed2009-07-20 13:48:07 +08002880 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03002881 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08002882 }
Dave Airlief453ba02008-11-07 14:05:41 -08002883
Matt Roper161d0dc2014-06-10 08:28:10 -07002884 /*
2885 * If this crtc has a universal cursor plane, call that plane's update
2886 * handler rather than using legacy cursor handlers.
2887 */
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01002888 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterf2b50c12014-09-12 17:07:32 +02002889 if (crtc->cursor) {
2890 ret = drm_mode_cursor_universal(crtc, req, file_priv);
2891 goto out;
2892 }
2893
Dave Airlief453ba02008-11-07 14:05:41 -08002894 if (req->flags & DRM_MODE_CURSOR_BO) {
Dave Airlie4c813d42013-06-20 11:48:52 +10002895 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
Dave Airlief453ba02008-11-07 14:05:41 -08002896 ret = -ENXIO;
2897 goto out;
2898 }
2899 /* Turns off the cursor if handle is 0 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002900 if (crtc->funcs->cursor_set2)
2901 ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
2902 req->width, req->height, req->hot_x, req->hot_y);
2903 else
2904 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
2905 req->width, req->height);
Dave Airlief453ba02008-11-07 14:05:41 -08002906 }
2907
2908 if (req->flags & DRM_MODE_CURSOR_MOVE) {
2909 if (crtc->funcs->cursor_move) {
2910 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
2911 } else {
Dave Airlief453ba02008-11-07 14:05:41 -08002912 ret = -EFAULT;
2913 goto out;
2914 }
2915 }
2916out:
Daniel Vetterd059f652014-07-25 18:07:40 +02002917 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +01002918
Dave Airlief453ba02008-11-07 14:05:41 -08002919 return ret;
Dave Airlie4c813d42013-06-20 11:48:52 +10002920
2921}
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002922
2923
2924/**
2925 * drm_mode_cursor_ioctl - set CRTC's cursor configuration
2926 * @dev: drm device for the ioctl
2927 * @data: data pointer for the ioctl
2928 * @file_priv: drm file for the ioctl call
2929 *
2930 * Set the cursor configuration based on user request.
2931 *
2932 * Called by the user via ioctl.
2933 *
2934 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002935 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002936 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002937int drm_mode_cursor_ioctl(struct drm_device *dev,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002938 void *data, struct drm_file *file_priv)
Dave Airlie4c813d42013-06-20 11:48:52 +10002939{
2940 struct drm_mode_cursor *req = data;
2941 struct drm_mode_cursor2 new_req;
2942
2943 memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
2944 new_req.hot_x = new_req.hot_y = 0;
2945
2946 return drm_mode_cursor_common(dev, &new_req, file_priv);
2947}
2948
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002949/**
2950 * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
2951 * @dev: drm device for the ioctl
2952 * @data: data pointer for the ioctl
2953 * @file_priv: drm file for the ioctl call
2954 *
2955 * Set the cursor configuration based on user request. This implements the 2nd
2956 * version of the cursor ioctl, which allows userspace to additionally specify
2957 * the hotspot of the pointer.
2958 *
2959 * Called by the user via ioctl.
2960 *
2961 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01002962 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002963 */
Dave Airlie4c813d42013-06-20 11:48:52 +10002964int drm_mode_cursor2_ioctl(struct drm_device *dev,
2965 void *data, struct drm_file *file_priv)
2966{
2967 struct drm_mode_cursor2 *req = data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01002968
Dave Airlie4c813d42013-06-20 11:48:52 +10002969 return drm_mode_cursor_common(dev, req, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08002970}
2971
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01002972/**
2973 * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
2974 * @bpp: bits per pixels
2975 * @depth: bit depth per pixel
2976 *
2977 * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
2978 * Useful in fbdev emulation code, since that deals in those values.
2979 */
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002980uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
2981{
2982 uint32_t fmt;
2983
2984 switch (bpp) {
2985 case 8:
Ville Syrjäläd84f0312013-01-31 19:43:38 +02002986 fmt = DRM_FORMAT_C8;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002987 break;
2988 case 16:
2989 if (depth == 15)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002990 fmt = DRM_FORMAT_XRGB1555;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002991 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02002992 fmt = DRM_FORMAT_RGB565;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002993 break;
2994 case 24:
Ville Syrjälä04b39242011-11-17 18:05:13 +02002995 fmt = DRM_FORMAT_RGB888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08002996 break;
2997 case 32:
2998 if (depth == 24)
Ville Syrjälä04b39242011-11-17 18:05:13 +02002999 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003000 else if (depth == 30)
Ville Syrjälä04b39242011-11-17 18:05:13 +02003001 fmt = DRM_FORMAT_XRGB2101010;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003002 else
Ville Syrjälä04b39242011-11-17 18:05:13 +02003003 fmt = DRM_FORMAT_ARGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003004 break;
3005 default:
Ville Syrjälä04b39242011-11-17 18:05:13 +02003006 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
3007 fmt = DRM_FORMAT_XRGB8888;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08003008 break;
3009 }
3010
3011 return fmt;
3012}
3013EXPORT_SYMBOL(drm_mode_legacy_fb_format);
3014
Daniel Vetter81065542016-06-21 10:54:13 +02003015static bool drm_property_type_valid(struct drm_property *property)
3016{
3017 if (property->flags & DRM_MODE_PROP_EXTENDED_TYPE)
3018 return !(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3019 return !!(property->flags & DRM_MODE_PROP_LEGACY_TYPE);
3020}
3021
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003022/**
3023 * drm_property_create - create a new property type
3024 * @dev: drm device
3025 * @flags: flags specifying the property type
3026 * @name: name of the property
3027 * @num_values: number of pre-defined values
3028 *
3029 * This creates a new generic drm property which can then be attached to a drm
3030 * object with drm_object_attach_property. The returned property object must be
3031 * freed with drm_property_destroy.
3032 *
Damien Lespiau3b5b9932014-10-31 14:39:11 +00003033 * Note that the DRM core keeps a per-device list of properties and that, if
3034 * drm_mode_config_cleanup() is called, it will destroy all properties created
3035 * by the driver.
3036 *
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003037 * Returns:
3038 * A pointer to the newly created property on success, NULL on failure.
3039 */
Dave Airlief453ba02008-11-07 14:05:41 -08003040struct drm_property *drm_property_create(struct drm_device *dev, int flags,
3041 const char *name, int num_values)
3042{
3043 struct drm_property *property = NULL;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003044 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003045
3046 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
3047 if (!property)
3048 return NULL;
3049
Rob Clark98f75de2014-05-30 11:37:03 -04003050 property->dev = dev;
3051
Dave Airlief453ba02008-11-07 14:05:41 -08003052 if (num_values) {
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01003053 property->values = kcalloc(num_values, sizeof(uint64_t),
3054 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08003055 if (!property->values)
3056 goto fail;
3057 }
3058
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003059 ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
3060 if (ret)
3061 goto fail;
3062
Dave Airlief453ba02008-11-07 14:05:41 -08003063 property->flags = flags;
3064 property->num_values = num_values;
Daniel Vetter3758b342014-11-19 18:38:10 +01003065 INIT_LIST_HEAD(&property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003066
Vinson Lee471dd2e2011-11-10 11:55:40 -08003067 if (name) {
Dave Airlief453ba02008-11-07 14:05:41 -08003068 strncpy(property->name, name, DRM_PROP_NAME_LEN);
Vinson Lee471dd2e2011-11-10 11:55:40 -08003069 property->name[DRM_PROP_NAME_LEN-1] = '\0';
3070 }
Dave Airlief453ba02008-11-07 14:05:41 -08003071
3072 list_add_tail(&property->head, &dev->mode_config.property_list);
Rob Clark5ea22f22014-05-30 11:34:01 -04003073
3074 WARN_ON(!drm_property_type_valid(property));
3075
Dave Airlief453ba02008-11-07 14:05:41 -08003076 return property;
3077fail:
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003078 kfree(property->values);
Dave Airlief453ba02008-11-07 14:05:41 -08003079 kfree(property);
3080 return NULL;
3081}
3082EXPORT_SYMBOL(drm_property_create);
3083
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003084/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003085 * drm_property_create_enum - create a new enumeration property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003086 * @dev: drm device
3087 * @flags: flags specifying the property type
3088 * @name: name of the property
3089 * @props: enumeration lists with property values
3090 * @num_values: number of pre-defined values
3091 *
3092 * This creates a new generic drm property which can then be attached to a drm
3093 * object with drm_object_attach_property. The returned property object must be
3094 * freed with drm_property_destroy.
3095 *
3096 * Userspace is only allowed to set one of the predefined values for enumeration
3097 * properties.
3098 *
3099 * Returns:
3100 * A pointer to the newly created property on success, NULL on failure.
3101 */
Sascha Hauer4a67d392012-02-06 10:58:17 +01003102struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
3103 const char *name,
3104 const struct drm_prop_enum_list *props,
3105 int num_values)
3106{
3107 struct drm_property *property;
3108 int i, ret;
3109
3110 flags |= DRM_MODE_PROP_ENUM;
3111
3112 property = drm_property_create(dev, flags, name, num_values);
3113 if (!property)
3114 return NULL;
3115
3116 for (i = 0; i < num_values; i++) {
3117 ret = drm_property_add_enum(property, i,
3118 props[i].type,
3119 props[i].name);
3120 if (ret) {
3121 drm_property_destroy(dev, property);
3122 return NULL;
3123 }
3124 }
3125
3126 return property;
3127}
3128EXPORT_SYMBOL(drm_property_create_enum);
3129
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003130/**
Thierry Reding2aa9d2b2014-06-26 21:37:20 +02003131 * drm_property_create_bitmask - create a new bitmask property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003132 * @dev: drm device
3133 * @flags: flags specifying the property type
3134 * @name: name of the property
3135 * @props: enumeration lists with property bitflags
Daniel Vetter295ee852014-07-30 14:23:44 +02003136 * @num_props: size of the @props array
3137 * @supported_bits: bitmask of all supported enumeration values
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003138 *
Daniel Vetter295ee852014-07-30 14:23:44 +02003139 * This creates a new bitmask drm property which can then be attached to a drm
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003140 * object with drm_object_attach_property. The returned property object must be
3141 * freed with drm_property_destroy.
3142 *
3143 * Compared to plain enumeration properties userspace is allowed to set any
3144 * or'ed together combination of the predefined property bitflag values
3145 *
3146 * Returns:
3147 * A pointer to the newly created property on success, NULL on failure.
3148 */
Rob Clark49e27542012-05-17 02:23:26 -06003149struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
3150 int flags, const char *name,
3151 const struct drm_prop_enum_list *props,
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303152 int num_props,
3153 uint64_t supported_bits)
Rob Clark49e27542012-05-17 02:23:26 -06003154{
3155 struct drm_property *property;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303156 int i, ret, index = 0;
3157 int num_values = hweight64(supported_bits);
Rob Clark49e27542012-05-17 02:23:26 -06003158
3159 flags |= DRM_MODE_PROP_BITMASK;
3160
3161 property = drm_property_create(dev, flags, name, num_values);
3162 if (!property)
3163 return NULL;
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303164 for (i = 0; i < num_props; i++) {
3165 if (!(supported_bits & (1ULL << props[i].type)))
3166 continue;
Rob Clark49e27542012-05-17 02:23:26 -06003167
Ville Syrjälä7689ffb2014-07-08 10:31:52 +05303168 if (WARN_ON(index >= num_values)) {
3169 drm_property_destroy(dev, property);
3170 return NULL;
3171 }
3172
3173 ret = drm_property_add_enum(property, index++,
Rob Clark49e27542012-05-17 02:23:26 -06003174 props[i].type,
3175 props[i].name);
3176 if (ret) {
3177 drm_property_destroy(dev, property);
3178 return NULL;
3179 }
3180 }
3181
3182 return property;
3183}
3184EXPORT_SYMBOL(drm_property_create_bitmask);
3185
Rob Clarkebc44cf2012-09-12 22:22:31 -05003186static struct drm_property *property_create_range(struct drm_device *dev,
3187 int flags, const char *name,
3188 uint64_t min, uint64_t max)
3189{
3190 struct drm_property *property;
3191
3192 property = drm_property_create(dev, flags, name, 2);
3193 if (!property)
3194 return NULL;
3195
3196 property->values[0] = min;
3197 property->values[1] = max;
3198
3199 return property;
3200}
3201
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003202/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003203 * drm_property_create_range - create a new unsigned ranged property type
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003204 * @dev: drm device
3205 * @flags: flags specifying the property type
3206 * @name: name of the property
3207 * @min: minimum value of the property
3208 * @max: maximum value of the property
3209 *
3210 * This creates a new generic drm property which can then be attached to a drm
3211 * object with drm_object_attach_property. The returned property object must be
3212 * freed with drm_property_destroy.
3213 *
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003214 * Userspace is allowed to set any unsigned integer value in the (min, max)
3215 * range inclusive.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003216 *
3217 * Returns:
3218 * A pointer to the newly created property on success, NULL on failure.
3219 */
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003220struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
3221 const char *name,
3222 uint64_t min, uint64_t max)
3223{
Rob Clarkebc44cf2012-09-12 22:22:31 -05003224 return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
3225 name, min, max);
Sascha Hauerd9bc3c02012-02-06 10:58:18 +01003226}
3227EXPORT_SYMBOL(drm_property_create_range);
3228
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003229/**
3230 * drm_property_create_signed_range - create a new signed ranged property type
3231 * @dev: drm device
3232 * @flags: flags specifying the property type
3233 * @name: name of the property
3234 * @min: minimum value of the property
3235 * @max: maximum value of the property
3236 *
3237 * This creates a new generic drm property which can then be attached to a drm
3238 * object with drm_object_attach_property. The returned property object must be
3239 * freed with drm_property_destroy.
3240 *
3241 * Userspace is allowed to set any signed integer value in the (min, max)
3242 * range inclusive.
3243 *
3244 * Returns:
3245 * A pointer to the newly created property on success, NULL on failure.
3246 */
Rob Clarkebc44cf2012-09-12 22:22:31 -05003247struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
3248 int flags, const char *name,
3249 int64_t min, int64_t max)
3250{
3251 return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
3252 name, I642U64(min), I642U64(max));
3253}
3254EXPORT_SYMBOL(drm_property_create_signed_range);
3255
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003256/**
3257 * drm_property_create_object - create a new object property type
3258 * @dev: drm device
3259 * @flags: flags specifying the property type
3260 * @name: name of the property
3261 * @type: object type from DRM_MODE_OBJECT_* defines
3262 *
3263 * This creates a new generic drm property which can then be attached to a drm
3264 * object with drm_object_attach_property. The returned property object must be
3265 * freed with drm_property_destroy.
3266 *
3267 * Userspace is only allowed to set this to any property value of the given
3268 * @type. Only useful for atomic properties, which is enforced.
3269 *
3270 * Returns:
3271 * A pointer to the newly created property on success, NULL on failure.
3272 */
Rob Clark98f75de2014-05-30 11:37:03 -04003273struct drm_property *drm_property_create_object(struct drm_device *dev,
3274 int flags, const char *name, uint32_t type)
3275{
3276 struct drm_property *property;
3277
3278 flags |= DRM_MODE_PROP_OBJECT;
3279
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003280 if (WARN_ON(!(flags & DRM_MODE_PROP_ATOMIC)))
3281 return NULL;
3282
Rob Clark98f75de2014-05-30 11:37:03 -04003283 property = drm_property_create(dev, flags, name, 1);
3284 if (!property)
3285 return NULL;
3286
3287 property->values[0] = type;
3288
3289 return property;
3290}
3291EXPORT_SYMBOL(drm_property_create_object);
3292
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003293/**
Daniel Vetter960cd9d2015-01-21 08:47:38 +01003294 * drm_property_create_bool - create a new boolean property type
3295 * @dev: drm device
3296 * @flags: flags specifying the property type
3297 * @name: name of the property
3298 *
3299 * This creates a new generic drm property which can then be attached to a drm
3300 * object with drm_object_attach_property. The returned property object must be
3301 * freed with drm_property_destroy.
3302 *
3303 * This is implemented as a ranged property with only {0, 1} as valid values.
3304 *
3305 * Returns:
3306 * A pointer to the newly created property on success, NULL on failure.
3307 */
3308struct drm_property *drm_property_create_bool(struct drm_device *dev, int flags,
3309 const char *name)
3310{
3311 return drm_property_create_range(dev, flags, name, 0, 1);
3312}
3313EXPORT_SYMBOL(drm_property_create_bool);
3314
3315/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003316 * drm_property_add_enum - add a possible value to an enumeration property
3317 * @property: enumeration property to change
3318 * @index: index of the new enumeration
3319 * @value: value of the new enumeration
3320 * @name: symbolic name of the new enumeration
3321 *
3322 * This functions adds enumerations to a property.
3323 *
3324 * It's use is deprecated, drivers should use one of the more specific helpers
3325 * to directly create the property with all enumerations already attached.
3326 *
3327 * Returns:
3328 * Zero on success, error code on failure.
3329 */
Dave Airlief453ba02008-11-07 14:05:41 -08003330int drm_property_add_enum(struct drm_property *property, int index,
3331 uint64_t value, const char *name)
3332{
3333 struct drm_property_enum *prop_enum;
3334
Rob Clark5ea22f22014-05-30 11:34:01 -04003335 if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3336 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
Rob Clark49e27542012-05-17 02:23:26 -06003337 return -EINVAL;
3338
3339 /*
3340 * Bitmask enum properties have the additional constraint of values
3341 * from 0 to 63
3342 */
Rob Clark5ea22f22014-05-30 11:34:01 -04003343 if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
3344 (value > 63))
Dave Airlief453ba02008-11-07 14:05:41 -08003345 return -EINVAL;
3346
Daniel Vetter3758b342014-11-19 18:38:10 +01003347 if (!list_empty(&property->enum_list)) {
3348 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003349 if (prop_enum->value == value) {
3350 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3351 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3352 return 0;
3353 }
3354 }
3355 }
3356
3357 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
3358 if (!prop_enum)
3359 return -ENOMEM;
3360
3361 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
3362 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
3363 prop_enum->value = value;
3364
3365 property->values[index] = value;
Daniel Vetter3758b342014-11-19 18:38:10 +01003366 list_add_tail(&prop_enum->head, &property->enum_list);
Dave Airlief453ba02008-11-07 14:05:41 -08003367 return 0;
3368}
3369EXPORT_SYMBOL(drm_property_add_enum);
3370
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003371/**
3372 * drm_property_destroy - destroy a drm property
3373 * @dev: drm device
3374 * @property: property to destry
3375 *
3376 * This function frees a property including any attached resources like
3377 * enumeration values.
3378 */
Dave Airlief453ba02008-11-07 14:05:41 -08003379void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
3380{
3381 struct drm_property_enum *prop_enum, *pt;
3382
Daniel Vetter3758b342014-11-19 18:38:10 +01003383 list_for_each_entry_safe(prop_enum, pt, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003384 list_del(&prop_enum->head);
3385 kfree(prop_enum);
3386 }
3387
3388 if (property->num_values)
3389 kfree(property->values);
Dave Airlie7c8f6d22016-04-15 15:10:32 +10003390 drm_mode_object_unregister(dev, &property->base);
Dave Airlief453ba02008-11-07 14:05:41 -08003391 list_del(&property->head);
3392 kfree(property);
3393}
3394EXPORT_SYMBOL(drm_property_destroy);
3395
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003396/**
3397 * drm_object_attach_property - attach a property to a modeset object
3398 * @obj: drm modeset object
3399 * @property: property to attach
3400 * @init_val: initial value of the property
3401 *
3402 * This attaches the given property to the modeset object with the given initial
3403 * value. Currently this function cannot fail since the properties are stored in
3404 * a statically sized array.
3405 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003406void drm_object_attach_property(struct drm_mode_object *obj,
3407 struct drm_property *property,
3408 uint64_t init_val)
3409{
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003410 int count = obj->properties->count;
Paulo Zanonic5431882012-05-15 18:09:02 -03003411
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003412 if (count == DRM_OBJECT_MAX_PROPERTY) {
3413 WARN(1, "Failed to attach object property (type: 0x%x). Please "
3414 "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
3415 "you see this message on the same object type.\n",
3416 obj->type);
3417 return;
Paulo Zanonic5431882012-05-15 18:09:02 -03003418 }
3419
Rob Clarkb17cd752014-12-16 18:05:30 -05003420 obj->properties->properties[count] = property;
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003421 obj->properties->values[count] = init_val;
3422 obj->properties->count++;
Rob Clark88a48e22014-12-18 16:01:50 -05003423 if (property->flags & DRM_MODE_PROP_ATOMIC)
3424 obj->properties->atomic_count++;
Paulo Zanonic5431882012-05-15 18:09:02 -03003425}
3426EXPORT_SYMBOL(drm_object_attach_property);
3427
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003428/**
3429 * drm_object_property_set_value - set the value of a property
3430 * @obj: drm mode object to set property value for
3431 * @property: property to set
3432 * @val: value the property should be set to
3433 *
3434 * This functions sets a given property on a given object. This function only
3435 * changes the software state of the property, it does not call into the
3436 * driver's ->set_property callback.
3437 *
3438 * Returns:
3439 * Zero on success, error code on failure.
3440 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003441int drm_object_property_set_value(struct drm_mode_object *obj,
3442 struct drm_property *property, uint64_t val)
3443{
3444 int i;
3445
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003446 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003447 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003448 obj->properties->values[i] = val;
3449 return 0;
3450 }
3451 }
3452
3453 return -EINVAL;
3454}
3455EXPORT_SYMBOL(drm_object_property_set_value);
3456
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003457/**
3458 * drm_object_property_get_value - retrieve the value of a property
3459 * @obj: drm mode object to get property value from
3460 * @property: property to retrieve
3461 * @val: storage for the property value
3462 *
3463 * This function retrieves the softare state of the given property for the given
3464 * property. Since there is no driver callback to retrieve the current property
3465 * value this might be out of sync with the hardware, depending upon the driver
3466 * and property.
3467 *
3468 * Returns:
3469 * Zero on success, error code on failure.
3470 */
Paulo Zanonic5431882012-05-15 18:09:02 -03003471int drm_object_property_get_value(struct drm_mode_object *obj,
3472 struct drm_property *property, uint64_t *val)
3473{
3474 int i;
3475
Rob Clark88a48e22014-12-18 16:01:50 -05003476 /* read-only properties bypass atomic mechanism and still store
3477 * their value in obj->properties->values[].. mostly to avoid
3478 * having to deal w/ EDID and similar props in atomic paths:
3479 */
3480 if (drm_core_check_feature(property->dev, DRIVER_ATOMIC) &&
3481 !(property->flags & DRM_MODE_PROP_IMMUTABLE))
3482 return drm_atomic_get_property(obj, property, val);
3483
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03003484 for (i = 0; i < obj->properties->count; i++) {
Rob Clarkb17cd752014-12-16 18:05:30 -05003485 if (obj->properties->properties[i] == property) {
Paulo Zanonic5431882012-05-15 18:09:02 -03003486 *val = obj->properties->values[i];
3487 return 0;
3488 }
3489 }
3490
3491 return -EINVAL;
3492}
3493EXPORT_SYMBOL(drm_object_property_get_value);
3494
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003495/**
Daniel Vetter1a498632014-11-19 18:38:09 +01003496 * drm_mode_getproperty_ioctl - get the property metadata
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003497 * @dev: DRM device
3498 * @data: ioctl data
3499 * @file_priv: DRM file info
3500 *
Daniel Vetter1a498632014-11-19 18:38:09 +01003501 * This function retrieves the metadata for a given property, like the different
3502 * possible values for an enum property or the limits for a range property.
3503 *
3504 * Blob properties are special
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003505 *
3506 * Called by the user via ioctl.
3507 *
3508 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003509 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003510 */
Dave Airlief453ba02008-11-07 14:05:41 -08003511int drm_mode_getproperty_ioctl(struct drm_device *dev,
3512 void *data, struct drm_file *file_priv)
3513{
Dave Airlief453ba02008-11-07 14:05:41 -08003514 struct drm_mode_get_property *out_resp = data;
3515 struct drm_property *property;
3516 int enum_count = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003517 int value_count = 0;
3518 int ret = 0, i;
3519 int copied;
3520 struct drm_property_enum *prop_enum;
3521 struct drm_mode_property_enum __user *enum_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003522 uint64_t __user *values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003523
Dave Airliefb3b06c2011-02-08 13:55:21 +10003524 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3525 return -EINVAL;
3526
Daniel Vetter84849902012-12-02 00:28:11 +01003527 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04003528 property = drm_property_find(dev, out_resp->prop_id);
3529 if (!property) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03003530 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003531 goto done;
3532 }
Dave Airlief453ba02008-11-07 14:05:41 -08003533
Rob Clark5ea22f22014-05-30 11:34:01 -04003534 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3535 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Daniel Vetter3758b342014-11-19 18:38:10 +01003536 list_for_each_entry(prop_enum, &property->enum_list, head)
Dave Airlief453ba02008-11-07 14:05:41 -08003537 enum_count++;
Dave Airlief453ba02008-11-07 14:05:41 -08003538 }
3539
3540 value_count = property->num_values;
3541
3542 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
3543 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
3544 out_resp->flags = property->flags;
3545
3546 if ((out_resp->count_values >= value_count) && value_count) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003547 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003548 for (i = 0; i < value_count; i++) {
3549 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
3550 ret = -EFAULT;
3551 goto done;
3552 }
3553 }
3554 }
3555 out_resp->count_values = value_count;
3556
Rob Clark5ea22f22014-05-30 11:34:01 -04003557 if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
3558 drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003559 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
3560 copied = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003561 enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
Daniel Vetter3758b342014-11-19 18:38:10 +01003562 list_for_each_entry(prop_enum, &property->enum_list, head) {
Dave Airlief453ba02008-11-07 14:05:41 -08003563
3564 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
3565 ret = -EFAULT;
3566 goto done;
3567 }
3568
3569 if (copy_to_user(&enum_ptr[copied].name,
3570 &prop_enum->name, DRM_PROP_NAME_LEN)) {
3571 ret = -EFAULT;
3572 goto done;
3573 }
3574 copied++;
3575 }
3576 }
3577 out_resp->count_enum_blobs = enum_count;
3578 }
3579
Daniel Vetter3758b342014-11-19 18:38:10 +01003580 /*
3581 * NOTE: The idea seems to have been to use this to read all the blob
3582 * property values. But nothing ever added them to the corresponding
3583 * list, userspace always used the special-purpose get_blob ioctl to
3584 * read the value for a blob property. It also doesn't make a lot of
3585 * sense to return values here when everything else is just metadata for
3586 * the property itself.
3587 */
3588 if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
3589 out_resp->count_enum_blobs = 0;
Dave Airlief453ba02008-11-07 14:05:41 -08003590done:
Daniel Vetter84849902012-12-02 00:28:11 +01003591 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08003592 return ret;
3593}
3594
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003595static void drm_property_free_blob(struct kref *kref)
3596{
3597 struct drm_property_blob *blob =
3598 container_of(kref, struct drm_property_blob, base.refcount);
3599
3600 mutex_lock(&blob->dev->mode_config.blob_lock);
3601 list_del(&blob->head_global);
3602 mutex_unlock(&blob->dev->mode_config.blob_lock);
3603
3604 drm_mode_object_unregister(blob->dev, &blob->base);
3605
3606 kfree(blob);
3607}
3608
Daniel Stone99531d92015-05-22 13:34:49 +01003609/**
3610 * drm_property_create_blob - Create new blob property
3611 *
3612 * Creates a new blob property for a specified DRM device, optionally
3613 * copying data.
3614 *
3615 * @dev: DRM device to create property for
3616 * @length: Length to allocate for blob data
3617 * @data: If specified, copies data into blob
Daniel Stone10e8cb72015-05-22 13:34:50 +01003618 *
3619 * Returns:
3620 * New blob property with a single reference on success, or an ERR_PTR
3621 * value on failure.
Daniel Stone99531d92015-05-22 13:34:49 +01003622 */
Daniel Stone6bcacf52015-04-20 19:22:55 +01003623struct drm_property_blob *
Thierry Redingecbbe592014-05-13 11:36:13 +02003624drm_property_create_blob(struct drm_device *dev, size_t length,
Thierry Reding12e6cec2014-05-13 11:38:36 +02003625 const void *data)
Dave Airlief453ba02008-11-07 14:05:41 -08003626{
3627 struct drm_property_blob *blob;
Ville Syrjälä6bfc56a2012-03-13 12:35:48 +02003628 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003629
Dan Carpenter9ac09342015-10-29 16:37:54 +03003630 if (!length || length > ULONG_MAX - sizeof(struct drm_property_blob))
Daniel Stone10e8cb72015-05-22 13:34:50 +01003631 return ERR_PTR(-EINVAL);
Dave Airlief453ba02008-11-07 14:05:41 -08003632
3633 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
3634 if (!blob)
Daniel Stone10e8cb72015-05-22 13:34:50 +01003635 return ERR_PTR(-ENOMEM);
Dave Airlief453ba02008-11-07 14:05:41 -08003636
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003637 /* This must be explicitly initialised, so we can safely call list_del
3638 * on it in the removal handler, even if it isn't in a file list. */
3639 INIT_LIST_HEAD(&blob->head_file);
Dave Airlief453ba02008-11-07 14:05:41 -08003640 blob->length = length;
Daniel Stone6bcacf52015-04-20 19:22:55 +01003641 blob->dev = dev;
Dave Airlief453ba02008-11-07 14:05:41 -08003642
Daniel Stone99531d92015-05-22 13:34:49 +01003643 if (data)
3644 memcpy(blob->data, data, length);
Dave Airlief453ba02008-11-07 14:05:41 -08003645
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003646 ret = drm_mode_object_get_reg(dev, &blob->base, DRM_MODE_OBJECT_BLOB,
3647 true, drm_property_free_blob);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01003648 if (ret) {
3649 kfree(blob);
Daniel Stone10e8cb72015-05-22 13:34:50 +01003650 return ERR_PTR(-EINVAL);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01003651 }
3652
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003653 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003654 list_add_tail(&blob->head_global,
3655 &dev->mode_config.property_blob_list);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01003656 mutex_unlock(&dev->mode_config.blob_lock);
3657
Dave Airlief453ba02008-11-07 14:05:41 -08003658 return blob;
3659}
Daniel Stone6bcacf52015-04-20 19:22:55 +01003660EXPORT_SYMBOL(drm_property_create_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08003661
Daniel Stone6bcacf52015-04-20 19:22:55 +01003662/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01003663 * drm_property_unreference_blob - Unreference a blob property
3664 *
3665 * Drop a reference on a blob property. May free the object.
3666 *
Daniel Stonef102c162015-05-22 13:34:44 +01003667 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01003668 */
3669void drm_property_unreference_blob(struct drm_property_blob *blob)
3670{
Daniel Stone6bcacf52015-04-20 19:22:55 +01003671 if (!blob)
3672 return;
3673
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003674 drm_mode_object_unreference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01003675}
3676EXPORT_SYMBOL(drm_property_unreference_blob);
3677
3678/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003679 * drm_property_destroy_user_blobs - destroy all blobs created by this client
3680 * @dev: DRM device
3681 * @file_priv: destroy all blobs owned by this file handle
3682 */
3683void drm_property_destroy_user_blobs(struct drm_device *dev,
3684 struct drm_file *file_priv)
3685{
3686 struct drm_property_blob *blob, *bt;
3687
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003688 /*
3689 * When the file gets released that means no one else can access the
3690 * blob list any more, so no need to grab dev->blob_lock.
3691 */
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003692 list_for_each_entry_safe(blob, bt, &file_priv->blobs, head_file) {
3693 list_del_init(&blob->head_file);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003694 drm_property_unreference_blob(blob);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003695 }
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003696}
3697
3698/**
Daniel Stone6bcacf52015-04-20 19:22:55 +01003699 * drm_property_reference_blob - Take a reference on an existing property
3700 *
3701 * Take a new reference on an existing blob property.
3702 *
Daniel Stonef102c162015-05-22 13:34:44 +01003703 * @blob: Pointer to blob property
Daniel Stone6bcacf52015-04-20 19:22:55 +01003704 */
3705struct drm_property_blob *drm_property_reference_blob(struct drm_property_blob *blob)
3706{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003707 drm_mode_object_reference(&blob->base);
Daniel Stone6bcacf52015-04-20 19:22:55 +01003708 return blob;
3709}
3710EXPORT_SYMBOL(drm_property_reference_blob);
3711
Daniel Stone6bcacf52015-04-20 19:22:55 +01003712/**
3713 * drm_property_lookup_blob - look up a blob property and take a reference
3714 * @dev: drm device
3715 * @id: id of the blob property
3716 *
3717 * If successful, this takes an additional reference to the blob property.
3718 * callers need to make sure to eventually unreference the returned property
3719 * again, using @drm_property_unreference_blob.
3720 */
3721struct drm_property_blob *drm_property_lookup_blob(struct drm_device *dev,
3722 uint32_t id)
Dave Airlief453ba02008-11-07 14:05:41 -08003723{
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003724 struct drm_mode_object *obj;
3725 struct drm_property_blob *blob = NULL;
Daniel Stone6bcacf52015-04-20 19:22:55 +01003726
Daniel Vetter7520a272016-08-15 16:07:02 +02003727 obj = __drm_mode_object_find(dev, id, DRM_MODE_OBJECT_BLOB);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003728 if (obj)
3729 blob = obj_to_blob(obj);
Daniel Stone6bcacf52015-04-20 19:22:55 +01003730 return blob;
3731}
3732EXPORT_SYMBOL(drm_property_lookup_blob);
3733
3734/**
Daniel Stoned2ed3432015-04-20 19:22:53 +01003735 * drm_property_replace_global_blob - atomically replace existing blob property
3736 * @dev: drm device
3737 * @replace: location of blob property pointer to be replaced
3738 * @length: length of data for new blob, or 0 for no data
3739 * @data: content for new blob, or NULL for no data
3740 * @obj_holds_id: optional object for property holding blob ID
3741 * @prop_holds_id: optional property holding blob ID
3742 * @return 0 on success or error on failure
3743 *
3744 * This function will atomically replace a global property in the blob list,
3745 * optionally updating a property which holds the ID of that property. It is
3746 * guaranteed to be atomic: no caller will be allowed to see intermediate
3747 * results, and either the entire operation will succeed and clean up the
3748 * previous property, or it will fail and the state will be unchanged.
3749 *
3750 * If length is 0 or data is NULL, no new blob will be created, and the holding
3751 * property, if specified, will be set to 0.
3752 *
3753 * Access to the replace pointer is assumed to be protected by the caller, e.g.
3754 * by holding the relevant modesetting object lock for its parent.
3755 *
3756 * For example, a drm_connector has a 'PATH' property, which contains the ID
3757 * of a blob property with the value of the MST path information. Calling this
3758 * function with replace pointing to the connector's path_blob_ptr, length and
3759 * data set for the new path information, obj_holds_id set to the connector's
3760 * base object, and prop_holds_id set to the path property name, will perform
3761 * a completely atomic update. The access to path_blob_ptr is protected by the
3762 * caller holding a lock on the connector.
3763 */
Daniel Vetteradebd6f2016-08-12 22:48:49 +02003764int drm_property_replace_global_blob(struct drm_device *dev,
3765 struct drm_property_blob **replace,
3766 size_t length,
3767 const void *data,
3768 struct drm_mode_object *obj_holds_id,
3769 struct drm_property *prop_holds_id)
Daniel Stoned2ed3432015-04-20 19:22:53 +01003770{
3771 struct drm_property_blob *new_blob = NULL;
3772 struct drm_property_blob *old_blob = NULL;
3773 int ret;
3774
3775 WARN_ON(replace == NULL);
3776
3777 old_blob = *replace;
3778
3779 if (length && data) {
3780 new_blob = drm_property_create_blob(dev, length, data);
Daniel Stone10e8cb72015-05-22 13:34:50 +01003781 if (IS_ERR(new_blob))
3782 return PTR_ERR(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01003783 }
3784
3785 /* This does not need to be synchronised with blob_lock, as the
3786 * get_properties ioctl locks all modesetting objects, and
3787 * obj_holds_id must be locked before calling here, so we cannot
3788 * have its value out of sync with the list membership modified
3789 * below under blob_lock. */
3790 if (obj_holds_id) {
3791 ret = drm_object_property_set_value(obj_holds_id,
3792 prop_holds_id,
3793 new_blob ?
3794 new_blob->base.id : 0);
3795 if (ret != 0)
3796 goto err_created;
3797 }
3798
Markus Elfringec530822015-07-05 21:55:10 +02003799 drm_property_unreference_blob(old_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01003800 *replace = new_blob;
3801
3802 return 0;
3803
3804err_created:
Daniel Stone6bcacf52015-04-20 19:22:55 +01003805 drm_property_unreference_blob(new_blob);
Daniel Stoned2ed3432015-04-20 19:22:53 +01003806 return ret;
Dave Airlief453ba02008-11-07 14:05:41 -08003807}
Daniel Vetteradebd6f2016-08-12 22:48:49 +02003808EXPORT_SYMBOL(drm_property_replace_global_blob);
Dave Airlief453ba02008-11-07 14:05:41 -08003809
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003810/**
3811 * drm_mode_getblob_ioctl - get the contents of a blob property value
3812 * @dev: DRM device
3813 * @data: ioctl data
3814 * @file_priv: DRM file info
3815 *
3816 * This function retrieves the contents of a blob property. The value stored in
3817 * an object's blob property is just a normal modeset object id.
3818 *
3819 * Called by the user via ioctl.
3820 *
3821 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003822 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003823 */
Dave Airlief453ba02008-11-07 14:05:41 -08003824int drm_mode_getblob_ioctl(struct drm_device *dev,
3825 void *data, struct drm_file *file_priv)
3826{
Dave Airlief453ba02008-11-07 14:05:41 -08003827 struct drm_mode_get_blob *out_resp = data;
3828 struct drm_property_blob *blob;
3829 int ret = 0;
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003830 void __user *blob_ptr;
Dave Airlief453ba02008-11-07 14:05:41 -08003831
Dave Airliefb3b06c2011-02-08 13:55:21 +10003832 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3833 return -EINVAL;
3834
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003835 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
3836 if (!blob)
3837 return -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08003838
3839 if (out_resp->length == blob->length) {
Ville Syrjälä81f6c7f2011-12-20 00:06:42 +02003840 blob_ptr = (void __user *)(unsigned long)out_resp->data;
Thierry Reding4dfd9092014-12-10 13:03:34 +01003841 if (copy_to_user(blob_ptr, blob->data, blob->length)) {
Dave Airlief453ba02008-11-07 14:05:41 -08003842 ret = -EFAULT;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003843 goto unref;
Dave Airlief453ba02008-11-07 14:05:41 -08003844 }
3845 }
3846 out_resp->length = blob->length;
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003847unref:
3848 drm_property_unreference_blob(blob);
Dave Airlief453ba02008-11-07 14:05:41 -08003849
Dave Airlief453ba02008-11-07 14:05:41 -08003850 return ret;
3851}
3852
Dave Airliecc7096f2014-10-22 12:03:04 +10003853/**
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003854 * drm_mode_createblob_ioctl - create a new blob property
3855 * @dev: DRM device
3856 * @data: ioctl data
3857 * @file_priv: DRM file info
3858 *
3859 * This function creates a new blob property with user-defined values. In order
3860 * to give us sensible validation and checking when creating, rather than at
3861 * every potential use, we also require a type to be provided upfront.
3862 *
3863 * Called by the user via ioctl.
3864 *
3865 * Returns:
3866 * Zero on success, negative errno on failure.
3867 */
3868int drm_mode_createblob_ioctl(struct drm_device *dev,
3869 void *data, struct drm_file *file_priv)
3870{
3871 struct drm_mode_create_blob *out_resp = data;
3872 struct drm_property_blob *blob;
3873 void __user *blob_ptr;
3874 int ret = 0;
3875
3876 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3877 return -EINVAL;
3878
3879 blob = drm_property_create_blob(dev, out_resp->length, NULL);
3880 if (IS_ERR(blob))
3881 return PTR_ERR(blob);
3882
3883 blob_ptr = (void __user *)(unsigned long)out_resp->data;
3884 if (copy_from_user(blob->data, blob_ptr, out_resp->length)) {
3885 ret = -EFAULT;
3886 goto out_blob;
3887 }
3888
3889 /* Dropping the lock between create_blob and our access here is safe
3890 * as only the same file_priv can remove the blob; at this point, it is
3891 * not associated with any file_priv. */
3892 mutex_lock(&dev->mode_config.blob_lock);
3893 out_resp->blob_id = blob->base.id;
Maneet Singh8731b262015-10-08 10:10:24 -04003894 list_add_tail(&blob->head_file, &file_priv->blobs);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003895 mutex_unlock(&dev->mode_config.blob_lock);
3896
3897 return 0;
3898
3899out_blob:
3900 drm_property_unreference_blob(blob);
3901 return ret;
3902}
3903
3904/**
3905 * drm_mode_destroyblob_ioctl - destroy a user blob property
3906 * @dev: DRM device
3907 * @data: ioctl data
3908 * @file_priv: DRM file info
3909 *
3910 * Destroy an existing user-defined blob property.
3911 *
3912 * Called by the user via ioctl.
3913 *
3914 * Returns:
3915 * Zero on success, negative errno on failure.
3916 */
3917int drm_mode_destroyblob_ioctl(struct drm_device *dev,
3918 void *data, struct drm_file *file_priv)
3919{
3920 struct drm_mode_destroy_blob *out_resp = data;
3921 struct drm_property_blob *blob = NULL, *bt;
3922 bool found = false;
3923 int ret = 0;
3924
3925 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3926 return -EINVAL;
3927
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003928 blob = drm_property_lookup_blob(dev, out_resp->blob_id);
3929 if (!blob)
3930 return -ENOENT;
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003931
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003932 mutex_lock(&dev->mode_config.blob_lock);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003933 /* Ensure the property was actually created by this user. */
3934 list_for_each_entry(bt, &file_priv->blobs, head_file) {
3935 if (bt == blob) {
3936 found = true;
3937 break;
3938 }
3939 }
3940
3941 if (!found) {
3942 ret = -EPERM;
3943 goto err;
3944 }
3945
3946 /* We must drop head_file here, because we may not be the last
3947 * reference on the blob. */
3948 list_del_init(&blob->head_file);
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003949 mutex_unlock(&dev->mode_config.blob_lock);
3950
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003951 /* One reference from lookup, and one from the filp. */
3952 drm_property_unreference_blob(blob);
3953 drm_property_unreference_blob(blob);
3954
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003955 return 0;
3956
3957err:
3958 mutex_unlock(&dev->mode_config.blob_lock);
Daniel Vetter152ef5f2016-04-22 22:10:30 +02003959 drm_property_unreference_blob(blob);
3960
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01003961 return ret;
3962}
3963
3964/**
Dave Airliecc7096f2014-10-22 12:03:04 +10003965 * drm_mode_connector_set_path_property - set tile property on connector
3966 * @connector: connector to set property on.
Daniel Stoned2ed3432015-04-20 19:22:53 +01003967 * @path: path to use for property; must not be NULL.
Dave Airliecc7096f2014-10-22 12:03:04 +10003968 *
3969 * This creates a property to expose to userspace to specify a
3970 * connector path. This is mainly used for DisplayPort MST where
3971 * connectors have a topology and we want to allow userspace to give
3972 * them more meaningful names.
3973 *
3974 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01003975 * Zero on success, negative errno on failure.
Dave Airliecc7096f2014-10-22 12:03:04 +10003976 */
Dave Airlie43aba7e2014-06-05 14:01:31 +10003977int drm_mode_connector_set_path_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02003978 const char *path)
Dave Airlie43aba7e2014-06-05 14:01:31 +10003979{
3980 struct drm_device *dev = connector->dev;
Thierry Redingecbbe592014-05-13 11:36:13 +02003981 int ret;
Dave Airlie43aba7e2014-06-05 14:01:31 +10003982
Daniel Stoned2ed3432015-04-20 19:22:53 +01003983 ret = drm_property_replace_global_blob(dev,
3984 &connector->path_blob_ptr,
3985 strlen(path) + 1,
3986 path,
3987 &connector->base,
3988 dev->mode_config.path_property);
Dave Airlie43aba7e2014-06-05 14:01:31 +10003989 return ret;
3990}
3991EXPORT_SYMBOL(drm_mode_connector_set_path_property);
3992
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01003993/**
Dave Airlie6f134d72014-10-20 16:30:50 +10003994 * drm_mode_connector_set_tile_property - set tile property on connector
3995 * @connector: connector to set property on.
3996 *
3997 * This looks up the tile information for a connector, and creates a
3998 * property for userspace to parse if it exists. The property is of
3999 * the form of 8 integers using ':' as a separator.
4000 *
4001 * Returns:
4002 * Zero on success, errno on failure.
4003 */
4004int drm_mode_connector_set_tile_property(struct drm_connector *connector)
4005{
4006 struct drm_device *dev = connector->dev;
Dave Airlie6f134d72014-10-20 16:30:50 +10004007 char tile[256];
Daniel Stoned2ed3432015-04-20 19:22:53 +01004008 int ret;
Dave Airlie6f134d72014-10-20 16:30:50 +10004009
4010 if (!connector->has_tile) {
Daniel Stoned2ed3432015-04-20 19:22:53 +01004011 ret = drm_property_replace_global_blob(dev,
4012 &connector->tile_blob_ptr,
4013 0,
4014 NULL,
4015 &connector->base,
4016 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004017 return ret;
4018 }
4019
4020 snprintf(tile, 256, "%d:%d:%d:%d:%d:%d:%d:%d",
4021 connector->tile_group->id, connector->tile_is_single_monitor,
4022 connector->num_h_tile, connector->num_v_tile,
4023 connector->tile_h_loc, connector->tile_v_loc,
4024 connector->tile_h_size, connector->tile_v_size);
Dave Airlie6f134d72014-10-20 16:30:50 +10004025
Daniel Stoned2ed3432015-04-20 19:22:53 +01004026 ret = drm_property_replace_global_blob(dev,
4027 &connector->tile_blob_ptr,
4028 strlen(tile) + 1,
4029 tile,
4030 &connector->base,
4031 dev->mode_config.tile_property);
Dave Airlie6f134d72014-10-20 16:30:50 +10004032 return ret;
4033}
4034EXPORT_SYMBOL(drm_mode_connector_set_tile_property);
4035
4036/**
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004037 * drm_mode_connector_update_edid_property - update the edid property of a connector
4038 * @connector: drm connector
4039 * @edid: new value of the edid property
4040 *
4041 * This function creates a new blob modeset object and assigns its id to the
4042 * connector's edid property.
4043 *
4044 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004045 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004046 */
Dave Airlief453ba02008-11-07 14:05:41 -08004047int drm_mode_connector_update_edid_property(struct drm_connector *connector,
Thierry Reding12e6cec2014-05-13 11:38:36 +02004048 const struct edid *edid)
Dave Airlief453ba02008-11-07 14:05:41 -08004049{
4050 struct drm_device *dev = connector->dev;
Daniel Stoned2ed3432015-04-20 19:22:53 +01004051 size_t size = 0;
Thierry Redingecbbe592014-05-13 11:36:13 +02004052 int ret;
Dave Airlief453ba02008-11-07 14:05:41 -08004053
Thomas Wood4cf2b282014-06-18 17:52:33 +01004054 /* ignore requests to set edid when overridden */
4055 if (connector->override_edid)
4056 return 0;
4057
Daniel Stoned2ed3432015-04-20 19:22:53 +01004058 if (edid)
Shixin Zenge24ff462015-07-03 08:46:50 +02004059 size = EDID_LENGTH * (1 + edid->extensions);
Dave Airlief453ba02008-11-07 14:05:41 -08004060
Daniel Stoned2ed3432015-04-20 19:22:53 +01004061 ret = drm_property_replace_global_blob(dev,
4062 &connector->edid_blob_ptr,
4063 size,
4064 edid,
4065 &connector->base,
4066 dev->mode_config.edid_property);
Dave Airlief453ba02008-11-07 14:05:41 -08004067 return ret;
4068}
4069EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
4070
Rob Clark3843e712014-12-16 18:05:29 -05004071/* Some properties could refer to dynamic refcnt'd objects, or things that
4072 * need special locking to handle lifetime issues (ie. to ensure the prop
4073 * value doesn't become invalid part way through the property update due to
4074 * race). The value returned by reference via 'obj' should be passed back
4075 * to drm_property_change_valid_put() after the property is set (and the
4076 * object to which the property is attached has a chance to take it's own
4077 * reference).
4078 */
Rob Clarkd34f20d2014-12-18 16:01:56 -05004079bool drm_property_change_valid_get(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004080 uint64_t value, struct drm_mode_object **ref)
Paulo Zanoni26a34812012-05-15 18:08:59 -03004081{
Thierry Reding2ca651d2014-12-10 13:03:39 +01004082 int i;
4083
Paulo Zanoni26a34812012-05-15 18:08:59 -03004084 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
4085 return false;
Rob Clark5ea22f22014-05-30 11:34:01 -04004086
Rob Clark3843e712014-12-16 18:05:29 -05004087 *ref = NULL;
4088
Rob Clark5ea22f22014-05-30 11:34:01 -04004089 if (drm_property_type_is(property, DRM_MODE_PROP_RANGE)) {
Paulo Zanoni26a34812012-05-15 18:08:59 -03004090 if (value < property->values[0] || value > property->values[1])
4091 return false;
4092 return true;
Rob Clarkebc44cf2012-09-12 22:22:31 -05004093 } else if (drm_property_type_is(property, DRM_MODE_PROP_SIGNED_RANGE)) {
4094 int64_t svalue = U642I64(value);
Thierry Reding4dfd9092014-12-10 13:03:34 +01004095
Rob Clarkebc44cf2012-09-12 22:22:31 -05004096 if (svalue < U642I64(property->values[0]) ||
4097 svalue > U642I64(property->values[1]))
4098 return false;
4099 return true;
Rob Clark5ea22f22014-05-30 11:34:01 -04004100 } else if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
Ville Syrjälä592c20e2012-05-24 20:53:58 +03004101 uint64_t valid_mask = 0;
Thierry Reding4dfd9092014-12-10 13:03:34 +01004102
Rob Clark49e27542012-05-17 02:23:26 -06004103 for (i = 0; i < property->num_values; i++)
4104 valid_mask |= (1ULL << property->values[i]);
4105 return !(value & ~valid_mask);
Rob Clark5ea22f22014-05-30 11:34:01 -04004106 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004107 struct drm_property_blob *blob;
4108
4109 if (value == 0)
4110 return true;
4111
4112 blob = drm_property_lookup_blob(property->dev, value);
4113 if (blob) {
4114 *ref = &blob->base;
4115 return true;
4116 } else {
4117 return false;
4118 }
Rob Clark98f75de2014-05-30 11:37:03 -04004119 } else if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Rob Clark98f75de2014-05-30 11:37:03 -04004120 /* a zero value for an object property translates to null: */
4121 if (value == 0)
4122 return true;
Rob Clark3843e712014-12-16 18:05:29 -05004123
Daniel Vetter7520a272016-08-15 16:07:02 +02004124 *ref = __drm_mode_object_find(property->dev, value,
4125 property->values[0]);
Tomi Valkeinen1e8985a2016-05-31 15:03:18 +03004126 return *ref != NULL;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004127 }
Thierry Reding2ca651d2014-12-10 13:03:39 +01004128
4129 for (i = 0; i < property->num_values; i++)
4130 if (property->values[i] == value)
4131 return true;
4132 return false;
Paulo Zanoni26a34812012-05-15 18:08:59 -03004133}
4134
Rob Clarkd34f20d2014-12-18 16:01:56 -05004135void drm_property_change_valid_put(struct drm_property *property,
Rob Clark3843e712014-12-16 18:05:29 -05004136 struct drm_mode_object *ref)
4137{
4138 if (!ref)
4139 return;
4140
4141 if (drm_property_type_is(property, DRM_MODE_PROP_OBJECT)) {
Dave Airlie027b3f8b2016-04-15 15:10:42 +10004142 drm_mode_object_unreference(ref);
Daniel Stoneda9b2a32015-05-25 19:11:49 +01004143 } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB))
4144 drm_property_unreference_blob(obj_to_blob(ref));
Rob Clark3843e712014-12-16 18:05:29 -05004145}
4146
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004147/**
4148 * drm_mode_connector_property_set_ioctl - set the current value of a connector property
4149 * @dev: DRM device
4150 * @data: ioctl data
4151 * @file_priv: DRM file info
4152 *
4153 * This function sets the current value for a connectors's property. It also
4154 * calls into a driver's ->set_property callback to update the hardware state
4155 *
4156 * Called by the user via ioctl.
4157 *
4158 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004159 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004160 */
Dave Airlief453ba02008-11-07 14:05:41 -08004161int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
4162 void *data, struct drm_file *file_priv)
4163{
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004164 struct drm_mode_connector_set_property *conn_set_prop = data;
4165 struct drm_mode_obj_set_property obj_set_prop = {
4166 .value = conn_set_prop->value,
4167 .prop_id = conn_set_prop->prop_id,
4168 .obj_id = conn_set_prop->connector_id,
4169 .obj_type = DRM_MODE_OBJECT_CONNECTOR
4170 };
Dave Airlief453ba02008-11-07 14:05:41 -08004171
Paulo Zanoni0057d8d2012-05-15 18:09:03 -03004172 /* It does all the locking and checking we need */
4173 return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
Dave Airlief453ba02008-11-07 14:05:41 -08004174}
4175
Paulo Zanonic5431882012-05-15 18:09:02 -03004176static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
4177 struct drm_property *property,
4178 uint64_t value)
4179{
4180 int ret = -EINVAL;
4181 struct drm_connector *connector = obj_to_connector(obj);
4182
4183 /* Do DPMS ourselves */
4184 if (property == connector->dev->mode_config.dpms_property) {
Daniel Vetter3558c112015-12-04 09:45:59 +01004185 ret = (*connector->funcs->dpms)(connector, (int)value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004186 } else if (connector->funcs->set_property)
4187 ret = connector->funcs->set_property(connector, property, value);
4188
4189 /* store the property value if successful */
4190 if (!ret)
Rob Clark58495562012-10-11 20:50:56 -05004191 drm_object_property_set_value(&connector->base, property, value);
Paulo Zanonic5431882012-05-15 18:09:02 -03004192 return ret;
4193}
4194
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004195static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
4196 struct drm_property *property,
4197 uint64_t value)
4198{
4199 int ret = -EINVAL;
4200 struct drm_crtc *crtc = obj_to_crtc(obj);
4201
4202 if (crtc->funcs->set_property)
4203 ret = crtc->funcs->set_property(crtc, property, value);
4204 if (!ret)
4205 drm_object_property_set_value(obj, property, value);
4206
4207 return ret;
4208}
4209
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004210/**
4211 * drm_mode_plane_set_obj_prop - set the value of a property
4212 * @plane: drm plane object to set property value for
4213 * @property: property to set
4214 * @value: value the property should be set to
4215 *
4216 * This functions sets a given property on a given plane object. This function
4217 * calls the driver's ->set_property callback and changes the software state of
4218 * the property if the callback succeeds.
4219 *
4220 * Returns:
4221 * Zero on success, error code on failure.
4222 */
4223int drm_mode_plane_set_obj_prop(struct drm_plane *plane,
4224 struct drm_property *property,
4225 uint64_t value)
Rob Clark4d939142012-05-17 02:23:27 -06004226{
4227 int ret = -EINVAL;
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004228 struct drm_mode_object *obj = &plane->base;
Rob Clark4d939142012-05-17 02:23:27 -06004229
4230 if (plane->funcs->set_property)
4231 ret = plane->funcs->set_property(plane, property, value);
4232 if (!ret)
4233 drm_object_property_set_value(obj, property, value);
4234
4235 return ret;
4236}
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004237EXPORT_SYMBOL(drm_mode_plane_set_obj_prop);
Rob Clark4d939142012-05-17 02:23:27 -06004238
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004239/**
Daniel Vetter1a498632014-11-19 18:38:09 +01004240 * drm_mode_obj_get_properties_ioctl - get the current value of a object's property
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004241 * @dev: DRM device
4242 * @data: ioctl data
4243 * @file_priv: DRM file info
4244 *
4245 * This function retrieves the current value for an object's property. Compared
4246 * to the connector specific ioctl this one is extended to also work on crtc and
4247 * plane objects.
4248 *
4249 * Called by the user via ioctl.
4250 *
4251 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004252 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004253 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004254int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
4255 struct drm_file *file_priv)
4256{
4257 struct drm_mode_obj_get_properties *arg = data;
4258 struct drm_mode_object *obj;
4259 int ret = 0;
Paulo Zanonic5431882012-05-15 18:09:02 -03004260
4261 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4262 return -EINVAL;
4263
Daniel Vetter84849902012-12-02 00:28:11 +01004264 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004265
4266 obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
4267 if (!obj) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004268 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004269 goto out;
4270 }
4271 if (!obj->properties) {
4272 ret = -EINVAL;
Daniel Vetter1649c332016-04-22 22:10:28 +02004273 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004274 }
4275
Rob Clark88a48e22014-12-18 16:01:50 -05004276 ret = get_properties(obj, file_priv->atomic,
Rob Clark95cbf112014-12-18 16:01:49 -05004277 (uint32_t __user *)(unsigned long)(arg->props_ptr),
4278 (uint64_t __user *)(unsigned long)(arg->prop_values_ptr),
4279 &arg->count_props);
Paulo Zanonic5431882012-05-15 18:09:02 -03004280
Daniel Vetter1649c332016-04-22 22:10:28 +02004281out_unref:
4282 drm_mode_object_unreference(obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004283out:
Daniel Vetter84849902012-12-02 00:28:11 +01004284 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004285 return ret;
4286}
4287
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004288/**
4289 * drm_mode_obj_set_property_ioctl - set the current value of an object's property
4290 * @dev: DRM device
4291 * @data: ioctl data
4292 * @file_priv: DRM file info
4293 *
4294 * This function sets the current value for an object's property. It also calls
4295 * into a driver's ->set_property callback to update the hardware state.
4296 * Compared to the connector specific ioctl this one is extended to also work on
4297 * crtc and plane objects.
4298 *
4299 * Called by the user via ioctl.
4300 *
4301 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004302 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004303 */
Paulo Zanonic5431882012-05-15 18:09:02 -03004304int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
4305 struct drm_file *file_priv)
4306{
4307 struct drm_mode_obj_set_property *arg = data;
4308 struct drm_mode_object *arg_obj;
4309 struct drm_mode_object *prop_obj;
4310 struct drm_property *property;
Rob Clark3843e712014-12-16 18:05:29 -05004311 int i, ret = -EINVAL;
4312 struct drm_mode_object *ref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004313
4314 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4315 return -EINVAL;
4316
Daniel Vetter84849902012-12-02 00:28:11 +01004317 drm_modeset_lock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004318
4319 arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004320 if (!arg_obj) {
4321 ret = -ENOENT;
Paulo Zanonic5431882012-05-15 18:09:02 -03004322 goto out;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004323 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004324 if (!arg_obj->properties)
Daniel Vetter1649c332016-04-22 22:10:28 +02004325 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004326
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004327 for (i = 0; i < arg_obj->properties->count; i++)
Rob Clarkb17cd752014-12-16 18:05:30 -05004328 if (arg_obj->properties->properties[i]->base.id == arg->prop_id)
Paulo Zanonic5431882012-05-15 18:09:02 -03004329 break;
4330
Paulo Zanoni7f88a9b2012-05-15 18:09:04 -03004331 if (i == arg_obj->properties->count)
Daniel Vetter1649c332016-04-22 22:10:28 +02004332 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004333
4334 prop_obj = drm_mode_object_find(dev, arg->prop_id,
4335 DRM_MODE_OBJECT_PROPERTY);
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004336 if (!prop_obj) {
4337 ret = -ENOENT;
Daniel Vetter1649c332016-04-22 22:10:28 +02004338 goto out_unref;
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004339 }
Paulo Zanonic5431882012-05-15 18:09:02 -03004340 property = obj_to_property(prop_obj);
4341
Rob Clark3843e712014-12-16 18:05:29 -05004342 if (!drm_property_change_valid_get(property, arg->value, &ref))
Dave Airlieb164d312016-04-27 11:10:09 +10004343 goto out_unref;
Paulo Zanonic5431882012-05-15 18:09:02 -03004344
4345 switch (arg_obj->type) {
4346 case DRM_MODE_OBJECT_CONNECTOR:
4347 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
4348 arg->value);
4349 break;
Paulo Zanonibffd9de02012-05-15 18:09:05 -03004350 case DRM_MODE_OBJECT_CRTC:
4351 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
4352 break;
Rob Clark4d939142012-05-17 02:23:27 -06004353 case DRM_MODE_OBJECT_PLANE:
Thomas Wood3a5f87c2014-08-20 14:45:00 +01004354 ret = drm_mode_plane_set_obj_prop(obj_to_plane(arg_obj),
4355 property, arg->value);
Rob Clark4d939142012-05-17 02:23:27 -06004356 break;
Paulo Zanonic5431882012-05-15 18:09:02 -03004357 }
4358
Rob Clark3843e712014-12-16 18:05:29 -05004359 drm_property_change_valid_put(property, ref);
4360
Daniel Vetter1649c332016-04-22 22:10:28 +02004361out_unref:
4362 drm_mode_object_unreference(arg_obj);
Paulo Zanonic5431882012-05-15 18:09:02 -03004363out:
Daniel Vetter84849902012-12-02 00:28:11 +01004364 drm_modeset_unlock_all(dev);
Paulo Zanonic5431882012-05-15 18:09:02 -03004365 return ret;
4366}
4367
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004368/**
4369 * drm_mode_connector_attach_encoder - attach a connector to an encoder
4370 * @connector: connector to attach
4371 * @encoder: encoder to attach @connector to
4372 *
4373 * This function links up a connector to an encoder. Note that the routing
4374 * restrictions between encoders and crtcs are exposed to userspace through the
4375 * possible_clones and possible_crtcs bitmasks.
4376 *
4377 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004378 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004379 */
Dave Airlief453ba02008-11-07 14:05:41 -08004380int drm_mode_connector_attach_encoder(struct drm_connector *connector,
4381 struct drm_encoder *encoder)
4382{
4383 int i;
4384
Thierry Redingeb47fe82015-11-16 18:19:53 +01004385 /*
4386 * In the past, drivers have attempted to model the static association
4387 * of connector to encoder in simple connector/encoder devices using a
4388 * direct assignment of connector->encoder = encoder. This connection
4389 * is a logical one and the responsibility of the core, so drivers are
4390 * expected not to mess with this.
4391 *
4392 * Note that the error return should've been enough here, but a large
4393 * majority of drivers ignores the return value, so add in a big WARN
4394 * to get people's attention.
4395 */
4396 if (WARN_ON(connector->encoder))
4397 return -EINVAL;
4398
Dave Airlief453ba02008-11-07 14:05:41 -08004399 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
4400 if (connector->encoder_ids[i] == 0) {
4401 connector->encoder_ids[i] = encoder->base.id;
4402 return 0;
4403 }
4404 }
4405 return -ENOMEM;
4406}
4407EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
4408
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004409/**
4410 * drm_mode_crtc_set_gamma_size - set the gamma table size
4411 * @crtc: CRTC to set the gamma table size for
4412 * @gamma_size: size of the gamma table
4413 *
4414 * Drivers which support gamma tables should set this to the supported gamma
4415 * table size when initializing the CRTC. Currently the drm core only supports a
4416 * fixed gamma table size.
4417 *
4418 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004419 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004420 */
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004421int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004422 int gamma_size)
Dave Airlief453ba02008-11-07 14:05:41 -08004423{
Daniel Vettercf48e292016-03-30 11:51:16 +02004424 uint16_t *r_base, *g_base, *b_base;
4425 int i;
4426
Dave Airlief453ba02008-11-07 14:05:41 -08004427 crtc->gamma_size = gamma_size;
4428
Thierry Redingbd3f0ff2014-12-10 13:03:35 +01004429 crtc->gamma_store = kcalloc(gamma_size, sizeof(uint16_t) * 3,
4430 GFP_KERNEL);
Dave Airlief453ba02008-11-07 14:05:41 -08004431 if (!crtc->gamma_store) {
4432 crtc->gamma_size = 0;
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004433 return -ENOMEM;
Dave Airlief453ba02008-11-07 14:05:41 -08004434 }
4435
Daniel Vettercf48e292016-03-30 11:51:16 +02004436 r_base = crtc->gamma_store;
4437 g_base = r_base + gamma_size;
4438 b_base = g_base + gamma_size;
4439 for (i = 0; i < gamma_size; i++) {
4440 r_base[i] = i << 8;
4441 g_base[i] = i << 8;
4442 b_base[i] = i << 8;
4443 }
4444
4445
Sascha Hauer4cae5b82012-02-01 11:38:23 +01004446 return 0;
Dave Airlief453ba02008-11-07 14:05:41 -08004447}
4448EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
4449
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004450/**
4451 * drm_mode_gamma_set_ioctl - set the gamma table
4452 * @dev: DRM device
4453 * @data: ioctl data
4454 * @file_priv: DRM file info
4455 *
4456 * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
4457 * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
4458 *
4459 * Called by the user via ioctl.
4460 *
4461 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004462 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004463 */
Dave Airlief453ba02008-11-07 14:05:41 -08004464int drm_mode_gamma_set_ioctl(struct drm_device *dev,
4465 void *data, struct drm_file *file_priv)
4466{
4467 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004468 struct drm_crtc *crtc;
4469 void *r_base, *g_base, *b_base;
4470 int size;
4471 int ret = 0;
4472
Dave Airliefb3b06c2011-02-08 13:55:21 +10004473 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4474 return -EINVAL;
4475
Daniel Vetter84849902012-12-02 00:28:11 +01004476 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004477 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4478 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004479 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004480 goto out;
4481 }
Dave Airlief453ba02008-11-07 14:05:41 -08004482
Laurent Pinchartebe0f242012-05-17 13:27:24 +02004483 if (crtc->funcs->gamma_set == NULL) {
4484 ret = -ENOSYS;
4485 goto out;
4486 }
4487
Dave Airlief453ba02008-11-07 14:05:41 -08004488 /* memcpy into gamma store */
4489 if (crtc_lut->gamma_size != crtc->gamma_size) {
4490 ret = -EINVAL;
4491 goto out;
4492 }
4493
4494 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4495 r_base = crtc->gamma_store;
4496 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
4497 ret = -EFAULT;
4498 goto out;
4499 }
4500
4501 g_base = r_base + size;
4502 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
4503 ret = -EFAULT;
4504 goto out;
4505 }
4506
4507 b_base = g_base + size;
4508 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
4509 ret = -EFAULT;
4510 goto out;
4511 }
4512
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02004513 ret = crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
Dave Airlief453ba02008-11-07 14:05:41 -08004514
4515out:
Daniel Vetter84849902012-12-02 00:28:11 +01004516 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004517 return ret;
4518
4519}
4520
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004521/**
4522 * drm_mode_gamma_get_ioctl - get the gamma table
4523 * @dev: DRM device
4524 * @data: ioctl data
4525 * @file_priv: DRM file info
4526 *
4527 * Copy the current gamma table into the storage provided. This also provides
4528 * the gamma table size the driver expects, which can be used to size the
4529 * allocated storage.
4530 *
4531 * Called by the user via ioctl.
4532 *
4533 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004534 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004535 */
Dave Airlief453ba02008-11-07 14:05:41 -08004536int drm_mode_gamma_get_ioctl(struct drm_device *dev,
4537 void *data, struct drm_file *file_priv)
4538{
4539 struct drm_mode_crtc_lut *crtc_lut = data;
Dave Airlief453ba02008-11-07 14:05:41 -08004540 struct drm_crtc *crtc;
4541 void *r_base, *g_base, *b_base;
4542 int size;
4543 int ret = 0;
4544
Dave Airliefb3b06c2011-02-08 13:55:21 +10004545 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4546 return -EINVAL;
4547
Daniel Vetter84849902012-12-02 00:28:11 +01004548 drm_modeset_lock_all(dev);
Rob Clarka2b34e22013-10-05 16:36:52 -04004549 crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
4550 if (!crtc) {
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004551 ret = -ENOENT;
Dave Airlief453ba02008-11-07 14:05:41 -08004552 goto out;
4553 }
Dave Airlief453ba02008-11-07 14:05:41 -08004554
4555 /* memcpy into gamma store */
4556 if (crtc_lut->gamma_size != crtc->gamma_size) {
4557 ret = -EINVAL;
4558 goto out;
4559 }
4560
4561 size = crtc_lut->gamma_size * (sizeof(uint16_t));
4562 r_base = crtc->gamma_store;
4563 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
4564 ret = -EFAULT;
4565 goto out;
4566 }
4567
4568 g_base = r_base + size;
4569 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
4570 ret = -EFAULT;
4571 goto out;
4572 }
4573
4574 b_base = g_base + size;
4575 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
4576 ret = -EFAULT;
4577 goto out;
4578 }
4579out:
Daniel Vetter84849902012-12-02 00:28:11 +01004580 drm_modeset_unlock_all(dev);
Dave Airlief453ba02008-11-07 14:05:41 -08004581 return ret;
4582}
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004583
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004584/**
4585 * drm_mode_page_flip_ioctl - schedule an asynchronous fb update
4586 * @dev: DRM device
4587 * @data: ioctl data
4588 * @file_priv: DRM file info
4589 *
4590 * This schedules an asynchronous update on a given CRTC, called page flip.
4591 * Optionally a drm event is generated to signal the completion of the event.
4592 * Generic drivers cannot assume that a pageflip with changed framebuffer
4593 * properties (including driver specific metadata like tiling layout) will work,
4594 * but some drivers support e.g. pixel format changes through the pageflip
4595 * ioctl.
4596 *
4597 * Called by the user via ioctl.
4598 *
4599 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004600 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004601 */
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004602int drm_mode_page_flip_ioctl(struct drm_device *dev,
4603 void *data, struct drm_file *file_priv)
4604{
4605 struct drm_mode_crtc_page_flip *page_flip = data;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004606 struct drm_crtc *crtc;
Daniel Vetter3d30a592014-07-27 13:42:42 +02004607 struct drm_framebuffer *fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004608 struct drm_pending_vblank_event *e = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004609 int ret = -EINVAL;
4610
4611 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
4612 page_flip->reserved != 0)
4613 return -EINVAL;
4614
Keith Packard62f21042013-07-22 18:50:00 -07004615 if ((page_flip->flags & DRM_MODE_PAGE_FLIP_ASYNC) && !dev->mode_config.async_page_flip)
4616 return -EINVAL;
4617
Rob Clarka2b34e22013-10-05 16:36:52 -04004618 crtc = drm_crtc_find(dev, page_flip->crtc_id);
4619 if (!crtc)
Ville Syrjäläf27657f2013-10-17 13:35:00 +03004620 return -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004621
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01004622 drm_modeset_lock_crtc(crtc, crtc->primary);
Matt Roperf4510a22014-04-01 15:22:40 -07004623 if (crtc->primary->fb == NULL) {
Chris Wilson90c1efd2010-07-17 20:23:26 +01004624 /* The framebuffer is currently unbound, presumably
4625 * due to a hotplug event, that userspace has not
4626 * yet discovered.
4627 */
4628 ret = -EBUSY;
4629 goto out;
4630 }
4631
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004632 if (crtc->funcs->page_flip == NULL)
4633 goto out;
4634
Daniel Vetter786b99e2012-12-02 21:53:40 +01004635 fb = drm_framebuffer_lookup(dev, page_flip->fb_id);
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004636 if (!fb) {
4637 ret = -ENOENT;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004638 goto out;
Ville Syrjälä37c4e702013-10-17 13:35:01 +03004639 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004640
Ville Syrjälä2afa701d2015-10-15 20:40:02 +03004641 if (crtc->state) {
4642 const struct drm_plane_state *state = crtc->primary->state;
4643
4644 ret = check_src_coords(state->src_x, state->src_y,
4645 state->src_w, state->src_h, fb);
4646 } else {
4647 ret = drm_crtc_check_viewport(crtc, crtc->x, crtc->y, &crtc->mode, fb);
4648 }
Damien Lespiauc11e9282013-09-25 16:45:30 +01004649 if (ret)
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004650 goto out;
Ville Syrjälä5f61bb42012-03-13 12:35:45 +02004651
Matt Roperf4510a22014-04-01 15:22:40 -07004652 if (crtc->primary->fb->pixel_format != fb->pixel_format) {
Laurent Pinchart909d9cd2013-04-22 01:38:46 +02004653 DRM_DEBUG_KMS("Page flip is not allowed to change frame buffer format.\n");
4654 ret = -EINVAL;
4655 goto out;
4656 }
4657
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004658 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01004659 e = kzalloc(sizeof *e, GFP_KERNEL);
4660 if (!e) {
4661 ret = -ENOMEM;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004662 goto out;
4663 }
Jesse Barnes7bd4d7b2009-11-19 10:50:22 -08004664 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
Thierry Redingf76511b2014-12-10 13:03:40 +01004665 e->event.base.length = sizeof(e->event);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004666 e->event.user_data = page_flip->user_data;
Daniel Vetter2dd500f2016-01-11 22:40:56 +01004667 ret = drm_event_reserve_init(dev, file_priv, &e->base, &e->event.base);
4668 if (ret) {
4669 kfree(e);
4670 goto out;
4671 }
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004672 }
4673
Daniel Vetter3d30a592014-07-27 13:42:42 +02004674 crtc->primary->old_fb = crtc->primary->fb;
Keith Packarded8d1972013-07-22 18:49:58 -07004675 ret = crtc->funcs->page_flip(crtc, fb, e, page_flip->flags);
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004676 if (ret) {
Daniel Vetter2dd500f2016-01-11 22:40:56 +01004677 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT)
4678 drm_event_cancel_free(dev, &e->base);
Daniel Vetterb0d12322012-12-11 01:07:12 +01004679 /* Keep the old fb, don't unref it. */
Daniel Vetter3d30a592014-07-27 13:42:42 +02004680 crtc->primary->old_fb = NULL;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004681 } else {
Daniel Vetter3cb43cc2015-07-07 08:43:03 +02004682 crtc->primary->fb = fb;
Daniel Vetterb0d12322012-12-11 01:07:12 +01004683 /* Unref only the old framebuffer. */
4684 fb = NULL;
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004685 }
4686
4687out:
Daniel Vetterb0d12322012-12-11 01:07:12 +01004688 if (fb)
4689 drm_framebuffer_unreference(fb);
Daniel Vetter3d30a592014-07-27 13:42:42 +02004690 if (crtc->primary->old_fb)
4691 drm_framebuffer_unreference(crtc->primary->old_fb);
4692 crtc->primary->old_fb = NULL;
Daniel Vetterd059f652014-07-25 18:07:40 +02004693 drm_modeset_unlock_crtc(crtc);
Daniel Vetterb4d5e7d2012-12-11 16:59:31 +01004694
Kristian Høgsbergd91d8a32009-11-17 12:43:55 -05004695 return ret;
4696}
Chris Wilsoneb033552011-01-24 15:11:08 +00004697
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004698/**
4699 * drm_mode_config_reset - call ->reset callbacks
4700 * @dev: drm device
4701 *
4702 * This functions calls all the crtc's, encoder's and connector's ->reset
4703 * callback. Drivers can use this in e.g. their driver load or resume code to
4704 * reset hardware and software state.
4705 */
Chris Wilsoneb033552011-01-24 15:11:08 +00004706void drm_mode_config_reset(struct drm_device *dev)
4707{
4708 struct drm_crtc *crtc;
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004709 struct drm_plane *plane;
Chris Wilsoneb033552011-01-24 15:11:08 +00004710 struct drm_encoder *encoder;
4711 struct drm_connector *connector;
4712
Daniel Vettere4f62542015-07-09 23:44:35 +02004713 drm_for_each_plane(plane, dev)
Daniel Vetter2a0d7cf2014-07-29 15:32:37 +02004714 if (plane->funcs->reset)
4715 plane->funcs->reset(plane);
4716
Daniel Vettere4f62542015-07-09 23:44:35 +02004717 drm_for_each_crtc(crtc, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00004718 if (crtc->funcs->reset)
4719 crtc->funcs->reset(crtc);
4720
Daniel Vettere4f62542015-07-09 23:44:35 +02004721 drm_for_each_encoder(encoder, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00004722 if (encoder->funcs->reset)
4723 encoder->funcs->reset(encoder);
4724
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02004725 mutex_lock(&dev->mode_config.mutex);
Dave Airlie4eebf602015-08-17 14:13:53 +10004726 drm_for_each_connector(connector, dev)
Chris Wilsoneb033552011-01-24 15:11:08 +00004727 if (connector->funcs->reset)
4728 connector->funcs->reset(connector);
Daniel Vetterf8c2ba32015-07-29 08:32:43 +02004729 mutex_unlock(&dev->mode_config.mutex);
Chris Wilsoneb033552011-01-24 15:11:08 +00004730}
4731EXPORT_SYMBOL(drm_mode_config_reset);
Dave Airlieff72145b2011-02-07 12:16:14 +10004732
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004733/**
4734 * drm_mode_create_dumb_ioctl - create a dumb backing storage buffer
4735 * @dev: DRM device
4736 * @data: ioctl data
4737 * @file_priv: DRM file info
4738 *
4739 * This creates a new dumb buffer in the driver's backing storage manager (GEM,
4740 * TTM or something else entirely) and returns the resulting buffer handle. This
4741 * handle can then be wrapped up into a framebuffer modeset object.
4742 *
4743 * Note that userspace is not allowed to use such objects for render
4744 * acceleration - drivers must create their own private ioctls for such a use
4745 * case.
4746 *
4747 * Called by the user via ioctl.
4748 *
4749 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004750 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004751 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004752int drm_mode_create_dumb_ioctl(struct drm_device *dev,
4753 void *data, struct drm_file *file_priv)
4754{
4755 struct drm_mode_create_dumb *args = data;
David Herrmannb28cd412014-01-20 20:09:55 +01004756 u32 cpp, stride, size;
Dave Airlieff72145b2011-02-07 12:16:14 +10004757
4758 if (!dev->driver->dumb_create)
4759 return -ENOSYS;
David Herrmannb28cd412014-01-20 20:09:55 +01004760 if (!args->width || !args->height || !args->bpp)
4761 return -EINVAL;
4762
4763 /* overflow checks for 32bit size calculations */
David Herrmann00e72082014-08-24 19:23:26 +02004764 /* NOTE: DIV_ROUND_UP() can overflow */
David Herrmannb28cd412014-01-20 20:09:55 +01004765 cpp = DIV_ROUND_UP(args->bpp, 8);
David Herrmann00e72082014-08-24 19:23:26 +02004766 if (!cpp || cpp > 0xffffffffU / args->width)
David Herrmannb28cd412014-01-20 20:09:55 +01004767 return -EINVAL;
4768 stride = cpp * args->width;
4769 if (args->height > 0xffffffffU / stride)
4770 return -EINVAL;
4771
4772 /* test for wrap-around */
4773 size = args->height * stride;
4774 if (PAGE_ALIGN(size) == 0)
4775 return -EINVAL;
4776
Thierry Redingf6085952014-11-03 11:14:14 +01004777 /*
4778 * handle, pitch and size are output parameters. Zero them out to
4779 * prevent drivers from accidentally using uninitialized data. Since
4780 * not all existing userspace is clearing these fields properly we
4781 * cannot reject IOCTL with garbage in them.
4782 */
4783 args->handle = 0;
4784 args->pitch = 0;
4785 args->size = 0;
4786
Dave Airlieff72145b2011-02-07 12:16:14 +10004787 return dev->driver->dumb_create(file_priv, dev, args);
4788}
4789
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004790/**
4791 * drm_mode_mmap_dumb_ioctl - create an mmap offset for a dumb backing storage buffer
4792 * @dev: DRM device
4793 * @data: ioctl data
4794 * @file_priv: DRM file info
4795 *
4796 * Allocate an offset in the drm device node's address space to be able to
4797 * memory map a dumb buffer.
4798 *
4799 * Called by the user via ioctl.
4800 *
4801 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004802 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004803 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004804int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
4805 void *data, struct drm_file *file_priv)
4806{
4807 struct drm_mode_map_dumb *args = data;
4808
4809 /* call driver ioctl to get mmap offset */
4810 if (!dev->driver->dumb_map_offset)
4811 return -ENOSYS;
4812
4813 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
4814}
4815
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004816/**
4817 * drm_mode_destroy_dumb_ioctl - destroy a dumb backing strage buffer
4818 * @dev: DRM device
4819 * @data: ioctl data
4820 * @file_priv: DRM file info
4821 *
4822 * This destroys the userspace handle for the given dumb backing storage buffer.
4823 * Since buffer objects must be reference counted in the kernel a buffer object
4824 * won't be immediately freed if a framebuffer modeset object still uses it.
4825 *
4826 * Called by the user via ioctl.
4827 *
4828 * Returns:
Daniel Vetter1a498632014-11-19 18:38:09 +01004829 * Zero on success, negative errno on failure.
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004830 */
Dave Airlieff72145b2011-02-07 12:16:14 +10004831int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
4832 void *data, struct drm_file *file_priv)
4833{
4834 struct drm_mode_destroy_dumb *args = data;
4835
4836 if (!dev->driver->dumb_destroy)
4837 return -ENOSYS;
4838
4839 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
4840}
Dave Airlie248dbc22011-11-29 20:02:54 +00004841
Daniel Vetterc8e32cc2014-03-10 21:33:02 +01004842/**
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304843 * drm_rotation_simplify() - Try to simplify the rotation
4844 * @rotation: Rotation to be simplified
4845 * @supported_rotations: Supported rotations
4846 *
4847 * Attempt to simplify the rotation to a form that is supported.
4848 * Eg. if the hardware supports everything except DRM_REFLECT_X
4849 * one could call this function like this:
4850 *
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004851 * drm_rotation_simplify(rotation, DRM_ROTATE_0 |
4852 * DRM_ROTATE_90 | DRM_ROTATE_180 |
4853 * DRM_ROTATE_270 | DRM_REFLECT_Y);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304854 *
4855 * to eliminate the DRM_ROTATE_X flag. Depending on what kind of
4856 * transforms the hardware supports, this function may not
4857 * be able to produce a supported transform, so the caller should
4858 * check the result afterwards.
4859 */
4860unsigned int drm_rotation_simplify(unsigned int rotation,
4861 unsigned int supported_rotations)
4862{
4863 if (rotation & ~supported_rotations) {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004864 rotation ^= DRM_REFLECT_X | DRM_REFLECT_Y;
Joonas Lahtinen14152c82015-10-01 10:00:58 +03004865 rotation = (rotation & DRM_REFLECT_MASK) |
4866 BIT((ffs(rotation & DRM_ROTATE_MASK) + 1) % 4);
Ville Syrjälä3c9855f2014-07-08 10:31:56 +05304867 }
4868
4869 return rotation;
4870}
4871EXPORT_SYMBOL(drm_rotation_simplify);
4872
4873/**
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004874 * drm_mode_config_init - initialize DRM mode_configuration structure
4875 * @dev: DRM device
4876 *
4877 * Initialize @dev's mode_config structure, used for tracking the graphics
4878 * configuration of @dev.
4879 *
4880 * Since this initializes the modeset locks, no locking is possible. Which is no
4881 * problem, since this should happen single threaded at init time. It is the
4882 * driver's problem to ensure this guarantee.
4883 *
4884 */
4885void drm_mode_config_init(struct drm_device *dev)
4886{
4887 mutex_init(&dev->mode_config.mutex);
Rob Clark51fd3712013-11-19 12:10:12 -05004888 drm_modeset_lock_init(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004889 mutex_init(&dev->mode_config.idr_mutex);
4890 mutex_init(&dev->mode_config.fb_lock);
Daniel Stone8fb6e7a2015-04-20 19:22:54 +01004891 mutex_init(&dev->mode_config.blob_lock);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004892 INIT_LIST_HEAD(&dev->mode_config.fb_list);
4893 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
4894 INIT_LIST_HEAD(&dev->mode_config.connector_list);
4895 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
4896 INIT_LIST_HEAD(&dev->mode_config.property_list);
4897 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
4898 INIT_LIST_HEAD(&dev->mode_config.plane_list);
4899 idr_init(&dev->mode_config.crtc_idr);
Dave Airlie138f9eb2014-10-20 16:17:17 +10004900 idr_init(&dev->mode_config.tile_idr);
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01004901 ida_init(&dev->mode_config.connector_ida);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004902
4903 drm_modeset_lock_all(dev);
Rob Clark6b4959f2014-12-18 16:01:53 -05004904 drm_mode_create_standard_properties(dev);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004905 drm_modeset_unlock_all(dev);
4906
4907 /* Just to be sure */
4908 dev->mode_config.num_fb = 0;
4909 dev->mode_config.num_connector = 0;
4910 dev->mode_config.num_crtc = 0;
4911 dev->mode_config.num_encoder = 0;
Matt Ropere27dde32014-04-01 15:22:30 -07004912 dev->mode_config.num_overlay_plane = 0;
4913 dev->mode_config.num_total_plane = 0;
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004914}
4915EXPORT_SYMBOL(drm_mode_config_init);
4916
4917/**
4918 * drm_mode_config_cleanup - free up DRM mode_config info
4919 * @dev: DRM device
4920 *
4921 * Free up all the connectors and CRTCs associated with this DRM device, then
4922 * free up the framebuffers and associated buffer objects.
4923 *
4924 * Note that since this /should/ happen single-threaded at driver/device
4925 * teardown time, no locking is required. It's the driver's job to ensure that
4926 * this guarantee actually holds true.
4927 *
4928 * FIXME: cleanup any dangling user buffer objects too
4929 */
4930void drm_mode_config_cleanup(struct drm_device *dev)
4931{
4932 struct drm_connector *connector, *ot;
4933 struct drm_crtc *crtc, *ct;
4934 struct drm_encoder *encoder, *enct;
4935 struct drm_framebuffer *fb, *fbt;
4936 struct drm_property *property, *pt;
4937 struct drm_property_blob *blob, *bt;
4938 struct drm_plane *plane, *plt;
4939
4940 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
4941 head) {
4942 encoder->funcs->destroy(encoder);
4943 }
4944
4945 list_for_each_entry_safe(connector, ot,
4946 &dev->mode_config.connector_list, head) {
4947 connector->funcs->destroy(connector);
4948 }
4949
4950 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
4951 head) {
4952 drm_property_destroy(dev, property);
4953 }
4954
Maarten Lankhorstf35034f2016-03-22 15:42:14 +01004955 list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
4956 head) {
4957 plane->funcs->destroy(plane);
4958 }
4959
4960 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
4961 crtc->funcs->destroy(crtc);
4962 }
4963
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004964 list_for_each_entry_safe(blob, bt, &dev->mode_config.property_blob_list,
Daniel Stonee2f5d2e2015-05-22 13:34:51 +01004965 head_global) {
Daniel Stone6bcacf52015-04-20 19:22:55 +01004966 drm_property_unreference_blob(blob);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004967 }
4968
4969 /*
4970 * Single-threaded teardown context, so it's not required to grab the
4971 * fb_lock to protect against concurrent fb_list access. Contrary, it
4972 * would actually deadlock with the drm_framebuffer_cleanup function.
4973 *
4974 * Also, if there are any framebuffers left, that's a driver leak now,
4975 * so politely WARN about this.
4976 */
4977 WARN_ON(!list_empty(&dev->mode_config.fb_list));
4978 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
Dave Airlied0f37cf2016-04-15 15:10:36 +10004979 drm_framebuffer_free(&fb->base.refcount);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004980 }
4981
Maarten Lankhorst5fff80b2016-02-17 08:32:05 +01004982 ida_destroy(&dev->mode_config.connector_ida);
Dave Airlie138f9eb2014-10-20 16:17:17 +10004983 idr_destroy(&dev->mode_config.tile_idr);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004984 idr_destroy(&dev->mode_config.crtc_idr);
Rob Clark51fd3712013-11-19 12:10:12 -05004985 drm_modeset_lock_fini(&dev->mode_config.connection_mutex);
Laurent Pinchart87d24fc2013-04-15 15:37:16 +02004986}
4987EXPORT_SYMBOL(drm_mode_config_cleanup);
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05304988
4989struct drm_property *drm_mode_create_rotation_property(struct drm_device *dev,
4990 unsigned int supported_rotations)
4991{
4992 static const struct drm_prop_enum_list props[] = {
Joonas Lahtinen31ad61e2016-07-29 08:50:05 +03004993 { __builtin_ffs(DRM_ROTATE_0) - 1, "rotate-0" },
4994 { __builtin_ffs(DRM_ROTATE_90) - 1, "rotate-90" },
4995 { __builtin_ffs(DRM_ROTATE_180) - 1, "rotate-180" },
4996 { __builtin_ffs(DRM_ROTATE_270) - 1, "rotate-270" },
4997 { __builtin_ffs(DRM_REFLECT_X) - 1, "reflect-x" },
4998 { __builtin_ffs(DRM_REFLECT_Y) - 1, "reflect-y" },
Ville Syrjäläc1df5f32014-07-08 10:31:53 +05304999 };
5000
5001 return drm_property_create_bitmask(dev, 0, "rotation",
5002 props, ARRAY_SIZE(props),
5003 supported_rotations);
5004}
5005EXPORT_SYMBOL(drm_mode_create_rotation_property);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005006
5007/**
5008 * DOC: Tile group
5009 *
5010 * Tile groups are used to represent tiled monitors with a unique
5011 * integer identifier. Tiled monitors using DisplayID v1.3 have
5012 * a unique 8-byte handle, we store this in a tile group, so we
5013 * have a common identifier for all tiles in a monitor group.
5014 */
5015static void drm_tile_group_free(struct kref *kref)
5016{
5017 struct drm_tile_group *tg = container_of(kref, struct drm_tile_group, refcount);
5018 struct drm_device *dev = tg->dev;
5019 mutex_lock(&dev->mode_config.idr_mutex);
5020 idr_remove(&dev->mode_config.tile_idr, tg->id);
5021 mutex_unlock(&dev->mode_config.idr_mutex);
5022 kfree(tg);
5023}
5024
5025/**
5026 * drm_mode_put_tile_group - drop a reference to a tile group.
5027 * @dev: DRM device
5028 * @tg: tile group to drop reference to.
5029 *
5030 * drop reference to tile group and free if 0.
5031 */
5032void drm_mode_put_tile_group(struct drm_device *dev,
5033 struct drm_tile_group *tg)
5034{
5035 kref_put(&tg->refcount, drm_tile_group_free);
5036}
5037
5038/**
5039 * drm_mode_get_tile_group - get a reference to an existing tile group
5040 * @dev: DRM device
5041 * @topology: 8-bytes unique per monitor.
5042 *
5043 * Use the unique bytes to get a reference to an existing tile group.
5044 *
5045 * RETURNS:
5046 * tile group or NULL if not found.
5047 */
5048struct drm_tile_group *drm_mode_get_tile_group(struct drm_device *dev,
5049 char topology[8])
5050{
5051 struct drm_tile_group *tg;
5052 int id;
5053 mutex_lock(&dev->mode_config.idr_mutex);
5054 idr_for_each_entry(&dev->mode_config.tile_idr, tg, id) {
5055 if (!memcmp(tg->group_data, topology, 8)) {
5056 if (!kref_get_unless_zero(&tg->refcount))
5057 tg = NULL;
5058 mutex_unlock(&dev->mode_config.idr_mutex);
5059 return tg;
5060 }
5061 }
5062 mutex_unlock(&dev->mode_config.idr_mutex);
5063 return NULL;
5064}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005065EXPORT_SYMBOL(drm_mode_get_tile_group);
Dave Airlie138f9eb2014-10-20 16:17:17 +10005066
5067/**
5068 * drm_mode_create_tile_group - create a tile group from a displayid description
5069 * @dev: DRM device
5070 * @topology: 8-bytes unique per monitor.
5071 *
5072 * Create a tile group for the unique monitor, and get a unique
5073 * identifier for the tile group.
5074 *
5075 * RETURNS:
5076 * new tile group or error.
5077 */
5078struct drm_tile_group *drm_mode_create_tile_group(struct drm_device *dev,
5079 char topology[8])
5080{
5081 struct drm_tile_group *tg;
5082 int ret;
5083
5084 tg = kzalloc(sizeof(*tg), GFP_KERNEL);
5085 if (!tg)
5086 return ERR_PTR(-ENOMEM);
5087
5088 kref_init(&tg->refcount);
5089 memcpy(tg->group_data, topology, 8);
5090 tg->dev = dev;
5091
5092 mutex_lock(&dev->mode_config.idr_mutex);
5093 ret = idr_alloc(&dev->mode_config.tile_idr, tg, 1, 0, GFP_KERNEL);
5094 if (ret >= 0) {
5095 tg->id = ret;
5096 } else {
5097 kfree(tg);
5098 tg = ERR_PTR(ret);
5099 }
5100
5101 mutex_unlock(&dev->mode_config.idr_mutex);
5102 return tg;
5103}
Rob Clark81ddd1b2015-03-27 13:01:52 -04005104EXPORT_SYMBOL(drm_mode_create_tile_group);
Jyri Sarhaf8ed34a2016-06-07 15:09:14 +03005105
5106/**
5107 * drm_crtc_enable_color_mgmt - enable color management properties
5108 * @crtc: DRM CRTC
5109 * @degamma_lut_size: the size of the degamma lut (before CSC)
5110 * @has_ctm: whether to attach ctm_property for CSC matrix
5111 * @gamma_lut_size: the size of the gamma lut (after CSC)
5112 *
5113 * This function lets the driver enable the color correction
5114 * properties on a CRTC. This includes 3 degamma, csc and gamma
5115 * properties that userspace can set and 2 size properties to inform
5116 * the userspace of the lut sizes. Each of the properties are
5117 * optional. The gamma and degamma properties are only attached if
5118 * their size is not 0 and ctm_property is only attached if has_ctm is
5119 * true.
5120 */
5121void drm_crtc_enable_color_mgmt(struct drm_crtc *crtc,
5122 uint degamma_lut_size,
5123 bool has_ctm,
5124 uint gamma_lut_size)
5125{
5126 struct drm_device *dev = crtc->dev;
5127 struct drm_mode_config *config = &dev->mode_config;
5128
5129 if (degamma_lut_size) {
5130 drm_object_attach_property(&crtc->base,
5131 config->degamma_lut_property, 0);
5132 drm_object_attach_property(&crtc->base,
5133 config->degamma_lut_size_property,
5134 degamma_lut_size);
5135 }
5136
5137 if (has_ctm)
5138 drm_object_attach_property(&crtc->base,
5139 config->ctm_property, 0);
5140
5141 if (gamma_lut_size) {
5142 drm_object_attach_property(&crtc->base,
5143 config->gamma_lut_property, 0);
5144 drm_object_attach_property(&crtc->base,
5145 config->gamma_lut_size_property,
5146 gamma_lut_size);
5147 }
5148}
5149EXPORT_SYMBOL(drm_crtc_enable_color_mgmt);